Changing color of seaborn plot line

这一生的挚爱 提交于 2020-08-07 04:05:21

问题


I cant change color of a 2d line in seaborn. I have 2 lines in my plot and I want to assign different colors for both of them.

sns.set(style="whitegrid")
data = pd.DataFrame(result_prices, columns=['Size percentage increase'])
data2 = pd.DataFrame(result_sizes, columns=['Size percentage increase'])
sns_plot = sns.lineplot(data=data, color='red', linewidth=2.5)
sns_plot = sns.lineplot(data=data2, linewidth=2.5)
sns_plot.figure.savefig("size_percentage_increase.png")

But color='red' does not change the color, why?


回答1:


Using the color parameter only appears to work with Series objects. Since your dataframes seem to only be one column, you could create them as Series instead:

sns.set(style="whitegrid")
data = pd.Series(result_prices)
data2 = pd.Series(result_sizes)
sns_plot = sns.lineplot(data=data, color='red', linewidth=2.5)
sns_plot = sns.lineplot(data=data2, linewidth=2.5)
sns_plot.figure.savefig("size_percentage_increase.png")

Documentation has an example toward the end.


You could alternatively define palette:

sns.set(style="whitegrid")
data = pd.DataFrame(result_prices, columns=['Size percentage increase'])
data2 = pd.DataFrame(result_sizes, columns=['Size percentage increase'])
sns_plot = sns.lineplot(data=data, palette=['red'], linewidth=2.5)
sns_plot = sns.lineplot(data=data2, linewidth=2.5)
sns_plot.figure.savefig("size_percentage_increase.png")


来源:https://stackoverflow.com/questions/58432235/changing-color-of-seaborn-plot-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!