在 matplotlib 中混淆标签颜色
Messing labels colors in matplotlib
我正在从具有三个键的字典中绘制数据:
[u'Ferronikel', u'Nicromo', u'Alambre_1']
每一个都有几个参数,比如电阻、电压等等
所以我使用一个函数来轻松绘制值。
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()
绘制电压与电流 I 运行:
graficar_parametro('I','V')
并得到了这张图片:
但是颜色不对:
蓝点对应 'Alambre_1' 一个很好,但是黄点应该标记为 'Nicromo' 镍铁应该有红点而不是绿线。
我认为使用 sorted
可以解决问题,但并没有解决。
for alambre in sorted(alambres.keys()):
plt.legend(sorted(alambres.keys()))
一种方法是为 matplotlib 对象进行存储。你需要区分点图和线图。
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()
如果您希望点和线都在图例中表示,请像这样附加到 leg
:
leg.append((dots, line))
我正在从具有三个键的字典中绘制数据:
[u'Ferronikel', u'Nicromo', u'Alambre_1']
每一个都有几个参数,比如电阻、电压等等 所以我使用一个函数来轻松绘制值。
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()
绘制电压与电流 I 运行:
graficar_parametro('I','V')
并得到了这张图片:
但是颜色不对:
蓝点对应 'Alambre_1' 一个很好,但是黄点应该标记为 'Nicromo' 镍铁应该有红点而不是绿线。
我认为使用 sorted
可以解决问题,但并没有解决。
for alambre in sorted(alambres.keys()):
plt.legend(sorted(alambres.keys()))
一种方法是为 matplotlib 对象进行存储。你需要区分点图和线图。
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()
如果您希望点和线都在图例中表示,请像这样附加到 leg
:
leg.append((dots, line))