matplotlib:将图例标签放置在每条跟踪截取右侧Y轴的位置

2024-06-28 11:02:32 发布

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

我在xy散点图上绘制了多条线

我的调色板中的颜色不止这些,这意味着颜色开始循环

我曾使用过其他调色板,但可见性受到影响

我现在想探讨的一个想法是在每条线截取右侧y轴的点处添加图例标签

大概是这样的:

enter image description here

这是否可以通过matplotlib和/或seaborn实现


Tags: matplotlib颜色绘制标签seabornxy图例调色板
1条回答
网友
1楼 · 发布于 2024-06-28 11:02:32

快速一个,使用this question的另一个答案

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

names = ['foo', 'bar', 'foobar']
N_size = 100
fig, ax = plt.subplots(1, 1)
df = pd.DataFrame(map(lambda x: pd.Series(np.cumsum(np.random.rand(N_size) - 0.5), name=x), names)).T
df.plot(ax=ax)
ax2 = ax.twinx()
ax2.set_ylim(ax.get_ylim())
ax2.set_yticks([df[col].iloc[-1] for col in df.columns])
ax2.set_yticklabels(df.columns)
plt.show()

enter image description here

相关问题 更多 >