Pandas图两个轴标签重叠

2024-09-27 23:18:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我的绘图代码如下:

ax30 = df5.plot(kind="scatter", x='Data Numbers', y='y', color="red", label="67890")
ax33 = ax30.twinx()
df8.plot(kind="scatter", x='Data Numbers', y='y', color="blue", label="12345", ax=ax33, secondary_y = True)

ax30.set_ylabel("A")
ax33.set_ylabel("B")
ax30.set_xlabel("C")
ax30.set_title("DDD")
ax30.set_xlim([0,150])
ax30.set_ylim([0,40000])
ax33.set_xlim([0,150])
ax33.set_ylim([0,40000])

我试图用两个不同的y轴绘制一个数据,但似乎图例是重叠的。我怎样才能克服这个问题,把图例重新组合起来呢?目前的数字如下:

代码的当前结果数字


Tags: 代码dataplotlabelcolor图例setnumbers
1条回答
网友
1楼 · 发布于 2024-09-27 23:18:45

此解决方案应垂直堆叠标签,消除任何重叠:

ax30 = df5.plot(kind="scatter", x='Data Numbers', y='y', color="red", label="67890")
ax33 = df8.plot(kind="scatter", x='Data Numbers', y='y', color="blue", label="12345", ax=ax33, secondary_y = True)

plt.legend([ax30 , ax33], ['label_1', 'label_2'], loc=1)

相关问题 更多 >

    热门问题