首页 > 解决方案 > 带有 y 变量名称的图例,不仅是处理

问题描述

我正在用 python 中的 sns 做一个 2-y 轴图。我得到一个图例,报告我的数据集中的两种治疗方法,但我没有得到图例中 y 标签的名称。先感谢您。

数据框的例子是:

Obs / sampling time /replicate / y1 /  y2
1        midday        1          8   9     
2        midday        1          8.5 9.5
1        evening       2          4   5
2        evening       2          4.5 5

图例重复两次处理和 y 标签

这就是我的代码的样子:

sns.lineplot(x="Obs", y="y1",
             hue="sampling time", style="sampling time", ci=95, markers=True, color="red",
             dashes=False, legend="full", label = "y1", data=df)
ax.set_xlabel('time (min)', fontsize=14)
ax.set_ylabel('y1', fontsize=14, color="red")

ax2 = ax.twinx()

sns.lineplot(x="Obs", y="y2", hue="sampling time", style="sampling time", color="blue", markers=True,
                    legend ="full", ci=95, labels= "Ci", data=df)

ax2.set_ylabel('y2', fontsize=14, color="blue")

ax.get_legend_handles_labels()
ax2.get_legend_handles_labels()
plt.show()

标签: pythonlegend

解决方案


推荐阅读