英文:
How do I get a scatterplot with multiple background colors based on the split lines I already know?
问题
我有一个数据框,其中包含要使用seaborn库绘制的x和y值。但问题是,我需要根据已有的数据点来用多种颜色着色背景。例如:
# df - 表示包含x和y数据的数据框
split_points = [20, 40, 60, 80, 100] # 分割点的列表,该列表也可以更长(与示例保持一致)
sns.scatterplot(...) # 我需要帮助来获得下面指定的图表
代码片段看起来像上面那样。但通常我希望将split_points列表中提到的所有点的分割显示为不同的背景颜色。希望找到实现此目标的方法。
我打算做类似于这样的事情:
我从这个答案中获取了图像,以便更好地理解。
英文:
I have a dataframe in which I have x and y values to be plotted using seaborn library. But the cache here is that I need to color the background with multiple colors based on the data points I already have. for example
# df - represents the dataframe in which the x and y data are present
split_points = [20,40,60,80,100] # The list of split point, the list can be bigger as well ( keeping it same with the example)
sns.scatterplot(...) # I need help with this to get the below specified graph
The snippet of code looks something like above. But I would typically like to get the split of all the points mentioned in the split_points list as different background colors. Hoping to find the method to do the same.
I intend to do something like this.
I have taken the image from this answer for better understanding.
答案1
得分: 1
You can use axvspan()
函数来为每个区域添加背景颜色,同时使用 alpha
控制透明度和 zorder
将有颜色的矩形移到后面,以使线条位于顶部。对于图例,您可以使用 label
在每种情况下提供文本。以下是一个可工作的示例。希望这是您正在寻找的内容...
df = np.random.uniform(low=-10, high=5, size=(100,)) ##线条的数据
split_points = [20, 40, 60, 80, 100] # 分割点的列表,也可以更大(与示例保持一致)
split_colors = ['red', 'yellow', 'green', 'blue', 'pink'] ##每个区域的颜色
sns.lineplot(data=df, color='black', lw=0.75) ##绘制所需的线图
labels = [*'abcde'] ##定义图例的标签
## 对于每个条目,绘制带有颜色、透明度、zorder 和图例标签的矩形
for i in range(len(split_points)):
plt.axvspan(split_points[i] - 20, split_points[i], facecolor=split_colors[i], alpha=0.2, zorder=-10, label=labels[i])
plt.legend()
plt.show()
<details>
<summary>英文:</summary>
You can use `axvspan()` to add the background color for each region, along with `alpha` to control the transparency and `zorder` to move the colored rectangles to the back, so that the lines are on top. For the legend, you can use `label` to provide the text in each case. Below is a working example. Hope this is what you are looking for...
df=np.random.uniform(low=-10, high=5, size=(100,)) ## data for the line
split_points = [20,40,60,80,100] # The list of split point, the list can be bigger as well ( keeping it same with the example)
split_colors = ['red', 'yellow', 'green', 'blue', 'pink'] ## Colors for each region
sns.lineplot(data=df, color='black', lw=0.75) ## Draw the line plot you want
labels=[*'abcde'] ## Define the labels for the legend
For each entry, draw the rectangle along with the color, transparency, zorder and label for the legend
for i in range(len(split_points)):
plt.axvspan(split_points[i]-20, split_points[i], facecolor=split_colors[i], alpha=0.2, zorder=-10, label=labels[i])
plt.legend()
plt.show()
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/yAF1z.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论