如何将散点图和线图与匹配的颜色和单个图例结合起来
How to combine a scatter and line plot with matching colours and single legend
看下面的代码
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()
我希望同系列有相同的颜色/图例。 IE。线图中第一个系列的颜色应与散点图中的颜色相同。如何实现?
您需要在 scatterplot
中设置与 lineplot
中相同的调色板。或者在两种情况下都使用默认值(省略 palette=
)。
要组合图例,您可以使用元组图例处理程序 (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()
看下面的代码
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()
我希望同系列有相同的颜色/图例。 IE。线图中第一个系列的颜色应与散点图中的颜色相同。如何实现?
您需要在 scatterplot
中设置与 lineplot
中相同的调色板。或者在两种情况下都使用默认值(省略 palette=
)。
要组合图例,您可以使用元组图例处理程序 (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()