首页 > 解决方案 > 误差条图中图例中的符号颜色与图中的颜色不对应

问题描述

在以下示例中,data_3 图例中的颜色将与 plt.errorbar([..],color='red') 中设置的颜色不同。扼杀这只适用于某些符号和颜色组合。例如,红色方块在图例中将显示为黑色方块,但红色圆圈将正确显示在图例中。当图例位于图形之外时,这似乎只是一个问题。这是一个工作示例: import matplotlib.pyplot as plt import matplotlib import seaborn as sns sns.set() # 将绘图样式设置为 seaborn

    matplotlib.rcParams['legend.handlelength'] = 0
    matplotlib.rcParams['legend.markerscale'] = 1

    labels = ['data_1','data_2','data_3','data_4']
    symbols = ['o','D','s','D']
    fill_style = ['none','full','none','full']
    colours = ['MediumPurple','Maroon','red','yellowGreen']

    x=range(10)
    y=range(10)

    for i in range(len(symbols)):
         plt.errorbar(x,[a+i*2 for a in y],elinewidth=1,color=colours[i],fmt=symbols[i],label=labels[i],ms=4,fillstyle=fill_style[i],markeredgewidth=1.75,markeredgecolor=colours[i])

    plt.legend(loc='center left',bbox_to_anchor=(1, 0.5)) 

上述代码的结果

标签: pythonmatplotliberrorbar

解决方案


刚刚意识到这只是在 jupyter 笔记本中显示图形时的一个问题。当图形保存为 .pdf 或 .png 时,图例条目都会按原样显示,正如您从我提供的图形中看到的那样!


推荐阅读