【问题标题】:My figure legend colours do not match my graph line colours?我的图形图例颜色与我的图形线颜色不匹配?
【发布时间】:2019-09-02 09:59:31
【问题描述】:

我正在尝试在图表上绘制两条线,并且正在努力将我的图例颜色与图表线颜色相匹配。当我尝试为绘图上的线条分配颜色时,它只会改变图例,虽然它也改变了图表的线条颜色,但它们与图例不匹配!

这是我的代码的基础。

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()

这是我得到的: Output Graph

如您所见,颜色不匹配,但更重要的是,匹配的红线肯定应该是图表上的顶线。我已经通过打印列表 b 和 c 验证了这一点,所以我确定。

如果我删除“r-”和“b-”,我会得到下图:Output Graph 2

还是不对……

我是 python 和编码的新手,所以如果您需要更多信息,请告诉我。感谢您的帮助!

P.S 您可能会看到我的误差线也仅适用于图形的一半,因为 .sem() 会为我的 pandas 数据框中的某些值生成 NaN。我认为这可能是由于除以 0 错误,因为我的所有数据都是非常小的浮点数 - 但如果您有任何见解,也将不胜感激!

【问题讨论】:

    标签: python matplotlib legend


    【解决方案1】:

    错误线隐藏了您在图例中显示的线。您可以删除多余的图,并以相应的颜色绘制误差线(线)。所以不是

    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)
    

    使用

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

    【讨论】:

    • 非常感谢!我一直在为这个问题失去理智,这解决了它:)
    【解决方案2】:

    您基本上是在初始线图上绘制误差线。默认情况下,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)
    

    给出你拥有的这条蓝色线。这同样适用于第二个情节。

    只需添加一个线型即可停用连接错误栏点与ls=''的线

    下面的更正应该有效:

    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()
    

    【讨论】:

    • 非常感谢!我认为 ls=' ' 对于未来的图表也会派上用场。再次感谢!
    【解决方案3】:

    尝试在调用 plot 时设置标签并删除 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()
    

    发生的情况是颜色正确,但隐藏在误差线图后面。 plt.errorbar 绘制线条和错误。由于您在第一个图而不是第二个图上设置了颜色,因此颜色最终会有所不同。

    对于误差线,检查值是否都相同。在这种情况下,标准偏差将为零。

    您也可以使用seaborn,这可能会为您节省大量时间;-)

    【讨论】:

    • 谢谢!它们的价值并不相同。标准偏差的 .std() 也失败了,所以我会用我自己的函数来试试。我也会去看看seaborn!谢谢
    猜你喜欢
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多