简体   繁体   English

我的图例颜色与我的图形线条颜色不匹配?

[英]My figure legend colours do not match my graph line colours?

I'm trying to plot two lines on a graph and am struggling to match my legend colours to the graph line colours. 我正在尝试在图形上绘制两条线,并努力使我的图例颜色与图形线颜色匹配。 When I try to assign colours to lines on the plot it only changes the legend and although it does alter the graph's line colours too they do not match the legend! 当我尝试为绘图上的线条分配颜色时,它只会更改图例,尽管它确实会更改图形的线条颜色,但它们也不会与图例匹配!

Here's the basis of my code. 这是我的代码的基础。

import pandas as pd
import matplotlib.pyplot as plt

df_mated = pd.read_csv("file1.txt", sep='\t', header=0)   
df_mated['Average'] = df_mated.mean(axis=1)
df_mated['SEM'] = df_mated.loc[:, :'Average'].sem()
mated_E = df_mated['SEM'].tolist()
b = df_mated['Average'].tolist()
plot1, = plt.plot(x, b, 'r-')
plt.errorbar(x, b, xerr=None, yerr=mated_E)

df_unmated = pd.read_csv("file2.txt", sep='\t', header=0) 
df_unmated['Average'] = df_unmated.mean(axis=1)
df_unmated['SEM'] = df_unmated.loc[:, :'Average'].sem()
unmated_E = df_unmated['SEM'].tolist()
c = df_unmated['Average'].tolist()
plot2, = plt.plot(x, c, 'b-')
plt.errorbar(x, c, xerr=None, yerr=unmated_E)

plt.xlabel('Position')
plt.ylabel('Average Read Depth')
plt.legend([plot1,plot2],["Mated", "Unmated"])
plt.show()

Here's what I get: Output Graph 这就是我得到的: 输出图

As you can see the colours do not match but more importantly the mated red line is definitely supposed to be the top line on the graph. 如您所见,颜色不匹配,但更重要的是,配对的红线绝对应该是图表上的顶线。 I've verified this by printing the lists b and c so I am sure. 我已经通过打印列表b和c对此进行了验证,所以我确定。

If I remove the 'r-' and 'b-' I get the following graph: Output Graph 2 如果删除“ r-”和“ b-”,则会得到以下图形: Output Graph 2

Still not right... 还是不对...

I'm new to python and coding so let me know if you need any more information. 我是python和编码的新手,所以如果您需要更多信息,请告诉我。 Thanks for any help! 谢谢你的帮助!

PS As you might be able to see my error bars also only work for half the graph as .sem() produces NaN for certain values in my pandas dataframe. PS可能您会看到我的误差线也仅适用于一半的图形,因为.sem()为我的熊猫数据框中的某些值生成NaN。 I think this may be due to a division by 0 error as all my data are very small floats - but if you have any insights that would also be appreciated! 我认为这可能是由于除以0的错误,因为我的所有数据都是非常小的浮点数-但是,如果您有任何见解,也将不胜感激!

The errobar lines hide the lines you show in the legend. errobar线会隐藏您在图例中显示的线。 You can just remove the redundant plots and just plot the errobar (lines) in the respective color. 您可以只删除多余的图,而仅以相应的颜色绘制errobar(线)。 So instead of 所以代替

plot1, = plt.plot(x, b, 'r-')
plt.errorbar(x, b, xerr=None, yerr=mated_E)
# ...
plot2, = plt.plot(x, c, 'b-')
plt.errorbar(x, c, xerr=None, yerr=unmated_E)

use 采用

plot1, _, _ = plt.errorbar(x, b, xerr=None, yerr=mated_E, color="r")
# ...
plot2, _, _ = plt.errorbar(x, c, xerr=None, yerr=unmated_E, color="b")

You are basically plotting the error bar over the inital line plot. 您基本上是在初始线条图上绘制误差线。 As default the plt.errorbar is a line plot with error bar on each point. 默认情况下,plt.errorbar是在每个点上都有误差线的线图。

# Gives a red line plot
plot1, = plt.plot(x, b, 'r-')
# Gives a '#1f77b4' (default first color) line plot with error bars
plt.errorbar(x, b, xerr=None, yerr=mated_E)

Giving this blue-ish line that you have. 给出您的蓝线。 The same can be applied to the second plot. 同样可以应用于第二个情节。

Just add a linestyle to deactivate the line connecting the points for the errobar with ls='' 只需添加一种线型即可停用将ls=''的errobar点连接起来的线

The correction below should work: 下面的更正应该起作用:

import pandas as pd
import matplotlib.pyplot as plt

df_mated = pd.read_csv("file1.txt", sep='\t', header=0)   
df_mated['Average'] = df_mated.mean(axis=1)
df_mated['SEM'] = df_mated.loc[:, :'Average'].sem()
mated_E = df_mated['SEM'].tolist()
b = df_mated['Average'].tolist()
plot1, = plt.plot(x, b, 'r-')
# Plot only the y-errorbar, not the line connecting the datapoint
plt.errorbar(x, b, xerr=None, yerr=mated_E, ls='')

df_unmated = pd.read_csv("file2.txt", sep='\t', header=0) 
df_unmated['Average'] = df_unmated.mean(axis=1)
df_unmated['SEM'] = df_unmated.loc[:, :'Average'].sem()
unmated_E = df_unmated['SEM'].tolist()
c = df_unmated['Average'].tolist()
plot2, = plt.plot(x, c, 'b-')
# Plot only the y-errorbar, not the line connecting the datapoint
plt.errorbar(x, c, xerr=None, yerr=unmated_E, ls='')

plt.xlabel('Position')
plt.ylabel('Average Read Depth')
plt.legend([plot1,plot2],["Mated", "Unmated"])
plt.show()

Try setting the labels when you call plot and remove the plt.plot lines, then just call plt.legend() without arguments. 调用图并删除plt.plot尝试设置标签,然后仅调用不带参数的plt.legend()

plt.errorbar(x, b, xerr=None, yerr=mated_E, fmt='r-', label='Mated') 
plt.errorbar(x, c, xerr=None, yerr=unmated_E, fmt='b-', label='Unmated')
plt.legend()

What is happening is that the colors are correct, but hidden behind the errorbar plot. 发生的情况是颜色正确,但是隐藏在错误栏图的后面。 plt.errorbar plot both the line and the errors. plt.errorbar绘制线和错误。 Since you set the color on the first plot and not the second, the colors end up being different. 由于您是在第一个图上而不是第二个图上设置颜色,因此最终颜色会有所不同。

As for the error bars, check whether the values are all the same. 至于误差线,请检查值是否全部相同。 In that case, the standard deviation will be zero. 在这种情况下,标准偏差将为零。

You can also use seaborn which can potentially save you a lot of time ;-) 您也可以使用seaborn ,这可以节省大量时间;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM