问题
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