简体   繁体   English

matplotlib中的标签颜色混乱

[英]Messing labels colors in matplotlib

I'm plotting data from a dict which has three keys: 我正在从具有三个键的字典中绘制数据:

[u'Ferronikel', u'Nicromo', u'Alambre_1']

And each one of this have several params like resistances, voltage an a like so I'm using a function to plot the values easily. 而且每个参数都有几个参数,例如电阻,电压等,因此我正在使用一个函数轻松绘制值。

def graficar_parametro(x,y):

    d_unidades = {'I':'A','V':'V','R':'ohm','T':'C','P':'W/m'}
    for alambre in sorted(alambres.keys()):
        model = sklearn.linear_model.LinearRegression()


        X = alambres[alambre]['mediciones'][x].reshape(-1, 1)
        Y = alambres[alambre]['mediciones'][y].reshape(-1, 1)
        model.fit(X,Y)


        x_label = d_unidades[x]
        y_label = d_unidades[y]
        plt.legend(sorted(alambres.keys()))
        plt.xlabel(x_label)
        plt.ylabel(y_label)
        plt.plot(X,Y,'8',
                X, model.predict(X),'-')
    plt.title('Heating wires')

    plt.show()

To plot Voltage vs current I run: 要绘制电压与电流的关系曲线,请运行:

graficar_parametro('I','V')

And got this images: 并得到了这张图片:

在此处输入图片说明

But there the colors are wrong: 但是这里的颜色是错误的:

Blue dots correspond to 'Alambre_1' that one is fine, but yellow dots should be labeled as 'Nicromo' and ferronikel should have the red dots not a green line. 蓝点对应于“ Alambre_1”,表示它很好,但黄点应标记为“ Nicromo”,而ferronikel应将红点标记为绿线。

I thought to use sorted would solve the issue, but it doesn't fix it. 我认为使用sorted可以解决问题,但不能解决问题。

for alambre in sorted(alambres.keys()):
       plt.legend(sorted(alambres.keys()))

One way to do so is to make storage for matplotlib objects. 一种方法是为matplotlib对象存储。 And you need to distinguish dot- and line-plots. 并且您需要区分点图和线图。

def graficar_parametro(x,y):

d_unidades = {'I':'A','V':'V','R':'ohm','T':'C','P':'W/m'}
leg = [] # Storage for plots we want to legend
for alambre in sorted(alambres.keys()):
    model = sklearn.linear_model.LinearRegression()


    X = alambres[alambre]['mediciones'][x].reshape(-1, 1)
    Y = alambres[alambre]['mediciones'][y].reshape(-1, 1)
    model.fit(X,Y)


    x_label = d_unidades[x]
    y_label = d_unidades[y]

    plt.xlabel(x_label)
    plt.ylabel(y_label)
    dots, = plt.plot(X,Y,'8')
    line, = plt.plot(X, model.predict(X),'-')
    leg.append(line) # Choose what symbols will be represented in legend
plt.legend(leg, sorted(alambres.keys())) # Legend
plt.title('Heating wires')

plt.show()

If you want that both dots and lines were represented in legend, append to leg like this: 如果要在图例中同时显示点和线,则将其附加到leg如下所示:

leg.append((dots, line))

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

相关问题 matplotlib 中字符串标签到 colors 的自定义排序映射 - custom ordered mapping of string labels to colors in matplotlib Matplotlib 图例 colors 更改图例标签时更改 - Matplotlib Legend colors change when changing legend labels 从“c=...”参数自动创建 Matplotlib 图例颜色+标签 - Automatically create Matplotlib Legend colors+labels from “c=…” argument 为什么matplotlib显示的图形有三种以上的颜色,而我只有三个标签? - Why the figure showed by matplotlib has more than three colors while I have only three labels? 如何在 Matplotlib 的条形图中的图例上分配多个标签和 colors? - How do I assign multiple labels and colors on a legend in a bar graph in Matplotlib? 如何设置颜色条轮廓线的刻度,颜色和标签的限制f matplotlib - How do I set limits on ticks, colors, and labels for colorbar contourf matplotlib pandas,matplotlib:一种为子图分配相同颜色,相同列标签的线条样式的方法吗? - pandas, matplotlib: a way to assign same colors, line styles for same column labels across subplots? Matplotlib - 让颜色条与 plot 中的 colors 对齐并使用原始值作为标签 - Matplotlib - Getting colorbar to line up with colors in plot and to use original values as labels Pylab:将标签映射到颜色 - Pylab: map labels to colors 在matplotlib中绘制更多颜色的图 - Plotting with more colors in matplotlib
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM