[英]legend colors are not matching seaborn
我正在使用 sns.pointplot 並且由於沒有label
屬性我決定創建我的自定義圖例但我的問題是顏色不匹配。
我的數據框看起來像這樣:
deploy deployed_today_rent total_rent cum_deploy hourly percent cum_percent
10min
2019-10-01 05:30:00 6 0 0 6 0.000000 0.000000
2019-10-01 05:40:00 0 0 0 6 0.000000 0.000000
2019-10-01 05:50:00 6 0 0 12 0.000000 0.000000
2019-10-01 06:00:00 13 0 0 25 0.000000 0.000000
2019-10-01 06:10:00 0 0 0 25 0.000000 0.000000
2019-10-01 06:20:00 0 1 1 25 0.040000 0.040000
2019-10-01 06:30:00 0 0 0 25 0.000000 0.040000
2019-10-01 06:40:00 0 1 1 25 0.040000 0.080000
2019-10-01 06:50:00 1 1 1 26 0.038462 0.118462
fig,(ax1)= plt.subplots(nrows=1)
fig.set_size_inches(22,17)
sns.pointplot(data=test, x=test.index, y="total_rent", ax=ax1,color="blue", label="total")
sns.pointplot(data=test, x=test.index, y="deployed_today_rent", ax=ax1, color="green", label="deployed_rent")
sns.pointplot(data=test, x=test.index, y="cum_deploy", ax=ax1, color="#BEC647", label="cum_deploy")
ax1.legend(labels=["total", "deployed_rent", "cum_deploy"], fontsize=15)
plt.savefig("test.png", dpi=300, bbox_inches="tight");
它成功地創建了一個圖例,但是圖例中的顏色與線條不匹配。
從圖例指南中,您可以通過使用matplotlib.lines.Line2D
為每一行創建一個“代理藝術家”來做到這一點,就像這樣
from matplotlib.lines import Line2D
a = Line2D([], [], color=‘blue’, label=‘total’)
b = Line2D([], [], color=‘green’, label=‘deployed_rent’)
c = Line2D([], [], color=‘#BEC647’, label=‘cum_deploy’)
plt.legend(handles=[a, b, c])
這應該產生一個帶有默認寬度的三條不同顏色線及其各自標簽的圖例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.