gpt4 book ai didi

不透明度的python matplotlib图例

转载 作者:太空宇宙 更新时间:2023-11-03 14:05:06 25 4
gpt4 key购买 nike

Example Plot

x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 6, 4]
c = [(1, 0, 0, 1),(1, 0, 0, .8),(0, 0, 0, .5),(0, 0, 0, .8),(1, 0, 0, .3)]

plt.scatter(x,y,c=c,s=55)
plt.legend(handles=[mpatches.Patch(color='red',label='Type1'),
mpatches.Patch(color='black',label='Type2')])
plt.show()

我正在绘制一个与上述数据集有些相似的数据集。在我的数据集中,颜色代表数据点的分类,不透明度代表其误差的大小(数据集比较密集,使得错误条不可行。)

我想知道是否有可能创建某种不透明度图例,也许是一行不透明度从 0 到 1 不等的黑点,每个黑点都标有相关的错误。

谢谢!

最佳答案

您可以添加另一个以空散点图作为句柄的图例,其中散点的 alpha 值是变化的。

例如,要使用从 0 到 1 的 6 种不同的不透明度,您可以这样做:

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 6, 4]
c = [(1, 0, 0, 1),(1, 0, 0, .8),(0, 0, 0, .5),(0, 0, 0, .8),(1, 0, 0, .3)]

plt.scatter(x,y,c=c,s=55)
leg1 = plt.legend(handles=[mpatches.Patch(color='red',label='Type1'),
mpatches.Patch(color='black',label='Type2')], loc="upper left")
plt.gca().add_artist(leg1)

error = [0,.2,.4,.6,.8,1]
h = [plt.scatter([],[],s=55, c=(0,0,0,i)) for i in error]
plt.legend(h, error, loc="upper right")

plt.show()

enter image description here

关于不透明度的python matplotlib图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44509315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com