[英]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.