【问题标题】:matplotlib: place legend labels where each trace intercepts the right-hand y-axis [duplicate]matplotlib:将图例标签放置在每个轨迹与右侧 y 轴相交的位置 [重复]
【发布时间】:2020-02-19 22:43:30
【问题描述】:

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

我的调色板中的颜色数量超过了颜色的数量,这意味着颜色开始循环。

我曾尝试过使用其他调色板,但能见度受到影响。

我现在想探索的一个想法是在每条线与右侧 y 轴相交的点添加图例标签。

类似这样的:

这可以通过 matplotlib 和/或 seaborn 实现吗?

【问题讨论】:

  • 完全有可能,并且已经有关于该主题的问题和答案。

标签: python matplotlib seaborn


【解决方案1】:

快速一个,使用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()

【讨论】:

  • 谢谢。我不确定你为什么被否决,因为它完全符合我的要求!也许是因为您没有详细说明什么,特别是达到了我正在寻找的效果?我不确定,但还是很疯狂,谢谢!
猜你喜欢
  • 2020-05-11
  • 1970-01-01
  • 2023-03-30
  • 2023-01-27
  • 2019-07-10
  • 1970-01-01
  • 2012-05-08
  • 2021-01-31
相关资源
最近更新 更多