使用plotlib自定义颜色错误图
我想更改使用Plotlib完成此错误图中的颜色,以自定义选择。我将在标签0(data1和data3)上使用不同的颜色(例如绿色)的错误图,相对于右侧的颜色(假设绿色)(标签1,所以data 2 and data4,例如红色)。此外,我想区分这两个系列(蓝色和橙色带有不同色调的颜色)。我尝试了不同的方法,但没有设法自定义图。
import matplotlib.pyplot as plt
# create sample data
N = 2
data_1 = {
'x': list(range(N)),
'y': [2,3],
'yerr': [0.2,0.2]}
data_2 = {
'x': list(range(N)),
'y': [4,5],
'yerr': [0.4,0.2]}
data_3 = {
'x': list(range(N)),
'y': [6,10],
'yerr': [0.3,0.7]}
data_4 = {
'x': list(range(N)),
'y': [8.1,10.6],
'yerr': [0.1,0.8]}
# plot
# only errorbar
plt.subplot(121)
for data in [data_1, data_2]:
plt.errorbar(**data, fmt='o')
plt.xlim([-1, 2])
plt.xticks([0, 1])
plt.subplot(122)
for data in [data_3, data_4]:
plt.errorbar(**data, fmt='o')
plt.xlim([-1, 2])
plt.xticks([0, 1])
I would like to change the colors in this error plot done with Plotlib in order to customize the choice. I would have the errors plots at label 0 (data1 and data3) with a different color (let's say green) with respect to the ones on the right (label 1, so data 2 and data4 and let's say red). Moreover, I would like to differentiate the two series (blue and orange with different shades of the colors). I tried different methods but did not manage to customize the plot.
import matplotlib.pyplot as plt
# create sample data
N = 2
data_1 = {
'x': list(range(N)),
'y': [2,3],
'yerr': [0.2,0.2]}
data_2 = {
'x': list(range(N)),
'y': [4,5],
'yerr': [0.4,0.2]}
data_3 = {
'x': list(range(N)),
'y': [6,10],
'yerr': [0.3,0.7]}
data_4 = {
'x': list(range(N)),
'y': [8.1,10.6],
'yerr': [0.1,0.8]}
# plot
# only errorbar
plt.subplot(121)
for data in [data_1, data_2]:
plt.errorbar(**data, fmt='o')
plt.xlim([-1, 2])
plt.xticks([0, 1])
plt.subplot(122)
for data in [data_3, data_4]:
plt.errorbar(**data, fmt='o')
plt.xlim([-1, 2])
plt.xticks([0, 1])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用
绘图
或errorbar
绘制时,一系列点都具有相同的颜色。您可以使用c =<某些颜色>
来指定它。就您而言,您希望这一点具有不同的颜色。这可以通过两种方式完成:1)通过点的顺序迭代并选择颜色或2)使用plt.scatter
并提供一系列点。但是,后一种方法没有yerr
选项。一种解决方案是绘制两次,一个用于误差线,另一个用于标记。请参阅此示例,使用提供的相同数据:产生此图:
现在是选择所需颜色的问题。
回答评论:要使颜色变化,您需要直接提供它们或找到映射一些唯一信息的方法(例如,在数据帧中的一行的字段“名称”)。这是直接提供它们的两种方法:
结果图:
When plotting with
plot
orerrorbar
, a sequence of points all get the same color. You can specify it withc=<some color>
. In your case, you want this sequence of points to have different colors. This can be done in two ways: 1) Iterate through the sequence of points and select a color or 2) useplt.scatter
and provide a sequence of points. The latter method, however, doesn't have theyerr
option. One solution is to plot twice, one for the error bars, another for the markers. See this example, using the same data you provided:Yields this plot:
Now it's a matter of choosing the colors you want.
Answering the comment: To get the colors to vary, you'll need to supply them directly or find a way of mapping some unique information (say, a field "name" of a row in a dataframe). Here's two methods of supplying them directly:
and the resulting graph: