简体   繁体   English

如何将散点和线 plot 与匹配的颜色和单个图例结合起来

[英]How to combine a scatter and line plot with matching colours and single legend

Looking at the following code看下面的代码

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
plot_df = pd.DataFrame(index=np.arange(5), columns=["Series 1", "Series 2"], data=np.array([[1, 2],[2.4, 5],[4.1, 7.1],[5, 8.9],[5.2, 10]]))
plot_df_points = pd.DataFrame(index = [1.5, 2, 3.7], columns = ["Series 1", "Series 2"], data=np.array([[1.2, 3.4],[4.5, 6.9],[5.5, 9.6]]))
df = pd.DataFrame(plot_df.stack()).reset_index()
df.columns = ["x", "Series","y"]
df_points = pd.DataFrame(plot_df_points.stack()).reset_index()
df_points.columns = ["x", "Series","y"]
fig, ax = plt.subplots()
sns.lineplot(data=df,x="x",y="y", hue="Series",ax=ax,palette="rocket",linewidth=2.5)
sns.scatterplot(data=df_points, x="x", y="y", hue="Series", ax=ax,s=200)
plt.show()
plt.close()

I would like to have the same colour / legend for the same series.我想为同一系列有相同的颜色/图例。 Ie the colour of series one in the line plot should be the same as in the scatter plot.即,plot 行中的系列一的颜色应与散点图 plot 中的颜色相同。 How can this be achieved?如何做到这一点?

You need to set the same palette in the scatterplot as in the lineplot .您需要在lineplot scatterplot的调色板。 Or use the default in both cases (leaving out palette= ).或者在这两种情况下都使用默认值(省略palette= )。

To combine the legends, you can use the tuple legend handler ( HandlerTuple ).要组合图例,您可以使用元组图例处理程序 ( HandlerTuple )。

import matplotlib.pyplot as plt
from matplotlib.legend_handler import HandlerTuple
import seaborn as sns
import pandas as pd
import numpy as np

plot_df = pd.DataFrame(index=np.arange(5), columns=["Series 1", "Series 2"],
                       data=[[1, 2], [2.4, 5], [4.1, 7.1], [5, 8.9], [5.2, 10]])
plot_df_points = pd.DataFrame(index=[1.5, 2, 3.7], columns=["Series 1", "Series 2"],
                              data=[[1.2, 3.4], [4.5, 6.9], [5.5, 9.6]])
df = plot_df.rename_axis('x').reset_index().melt(id_vars='x', var_name='Series', value_name='y')
df_points = plot_df_points.rename_axis('x').reset_index().melt(id_vars='x', var_name='Series', value_name='y')

fig, ax = plt.subplots()
sns.lineplot(data=df, x="x", y="y", hue="Series", ax=ax, palette="rocket", linewidth=2.5)
sns.scatterplot(data=df_points, x="x", y="y", hue="Series", ax=ax, palette="rocket", s=200)

handles, labels = ax.get_legend_handles_labels()
ax.legend([tuple(handles[::2]), tuple(handles[1::2])], labels[:2], handlelength=3,
          handler_map={tuple: HandlerTuple(ndivide=None)})

plt.tight_layout()
plt.show()

sns.lineplot 和 sns.scatterplot 的组合图例,颜色相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM