python matplotlib.pyplot.legend如果在不同的计算机上运行,则生成不同的图例

wljmcqd8  于 2023-01-12  发布在  Python
关注(0)|答案(1)|浏览(68)

我编写了一个脚本来自动绘制线图,当我运行该脚本时,它在我的计算机上运行良好。但是当其他人尝试运行同一脚本时,图例使用不同的颜色。
Correct Image
Incorrect Image (Wrong Legend)
下面是脚本:

plt.figure(figsize=(15, 9)) 
plt.style.use('fivethirtyeight') 
chart = sns.lineplot(x = pd.to_datetime(buyer_comp_trend['week']).dt.strftime('%m/%d'), y = buyer_comp_trend['Composite Score'], color = '#0000ff', linewidth=7.) 
chart = sns.lineplot(x = pd.to_datetime(buyer_comp_trend_ce['week']).dt.strftime('%m/%d'), y = buyer_comp_trend_ce['Composite Score'], color = '#99ccff', linewidth=3.) 
chart = sns.lineplot(x = pd.to_datetime(buyer_comp_trend_ea['week']).dt.strftime('%m/%d'), y = buyer_comp_trend_ea['Composite Score'], color = '#990099', linewidth=3.) 
chart = sns.lineplot(x = pd.to_datetime(buyer_comp_trend_ma['week']).dt.strftime('%m/%d'), y = buyer_comp_trend_ma['Composite Score'], color = '#1f1f1f', linewidth=3.) 
chart = sns.lineplot(x = pd.to_datetime(buyer_comp_trend_ms['week']).dt.strftime('%m/%d'), y = buyer_comp_trend_ms['Composite Score'], color = '#00b050', linewidth=3.) 
chart = sns.lineplot(x = pd.to_datetime(buyer_comp_trend_sc['week']).dt.strftime('%m/%d'), y = buyer_comp_trend_sc['Composite Score'], color = '#ff66cc', linewidth=3.) 
chart = sns.lineplot(x = pd.to_datetime(buyer_comp_trend_so['week']).dt.strftime('%m/%d'), y = buyer_comp_trend_so['Composite Score'], color = '#996633', linewidth=3.) 
chart = sns.lineplot(x = pd.to_datetime(buyer_comp_trend_we['week']).dt.strftime('%m/%d'), y = buyer_comp_trend_we['Composite Score'], color = '#ff8205', linewidth=3.) 
plt.plot(pd.to_datetime(buyer_comp_trend['week']).dt.strftime('%m/%d'), buyer_comp_trend['Target'], c = '#808080', ls = '--') 
chart.set(title = 'HRX Composite Score (Region scores by week)', ylabel='', xlabel="", facecolor = '#ffffff') 
x = range(0, len(pd.to_datetime(buyer_comp_trend['week']).dt.strftime('%m/%d'))) 
plt.ylim(920, 980) 
plt.legend(labels = ['National', 'CE', 'EA', 'MA', 'MS', 'SC', 'SO', 'WE', 'Target (939)'], ncol=9)

我们尝试过将python更新到相同的版本,将matplotlib更新到相同的版本,但是相同的脚本在不同的计算机上仍然会产生不同的图像。

ix0qys7i

ix0qys7i1#

我终于弄明白了。这个问题是关于seaborn库的。由于某种原因,seaborn的最新版本(0.12.1)在某种程度上弄乱了图例中分配给每个组的颜色。在将seaborn降级到版本0.11.2后,这个问题得到了解决。
用于降级海运的命令为:

pip install --force-reinstall -v "seaborn==0.11.2"

相关问题