【问题标题】:Matplotlib, vertical space between legend symbolsMatplotlib,图例符号之间的垂直空间
【发布时间】:2020-12-11 11:28:14
【问题描述】:

我在自定义情节图例时遇到问题。我做了很多定制,但无法理解这一点。我希望符号(而不是标签)在图例中的间距相等。正如您在示例中看到的,图例中圆圈之间的空间随着圆圈变大而变小。 有任何想法吗? 另外,我怎样才能添加一个彩条(除了大小),小圆圈是浅红色(例如),大圆圈是蓝色(例如) 到目前为止,这是我的代码:

import pandas as pd
import matplotlib.pyplot as plt
from vega_datasets import data as vega_data
gap = pd.read_json(vega_data.gapminder.url)

df = gap.loc[gap['year'] == 2000]

fig, ax = plt.subplots(1, 1,figsize=[14,12])
ax=ax.scatter(df['life_expect'], df['fertility'], 
            s = df['pop']/100000,alpha=0.7, edgecolor="black",cmap="viridis")

plt.xlabel("X")
plt.ylabel("Y");
kw = dict(prop="sizes", num=6, color="lightgrey", markeredgecolor='black',markeredgewidth=2)
plt.legend(*ax.legend_elements(**kw),bbox_to_anchor=(1, 0),frameon=False,
                    loc="lower left",markerscale=1,ncol=1,borderpad=2,labelspacing=4,handletextpad=2)

plt.grid()
plt.show()

【问题讨论】:

  • 由于图例也自动链接到标记,我们不能通过设置*kw = dict(markerscale=None...)来改善这一点吗?
  • 不,它不会改变任何东西......

标签: python pandas matplotlib legend


【解决方案1】:

这有点棘手,但您可以测量图例元素并重新定位它们以保持恒定的中间距离。由于像素定位,绘图后无法调整大小。

我使用“Qt5Agg”后端测试了 PyCharm 中的代码。在 Jupyter 笔记本中,使用 %matplotlib inline%matplotlib notebook。我不确定它是否适用于所有环境。

请注意,ax.scatter 不返回 ax(例如 sns.scatterplot),而是创建的散点列表。

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.transforms import IdentityTransform
from vega_datasets import data as vega_data

gap = pd.read_json(vega_data.gapminder.url)
df = gap.loc[gap['year'] == 2000]

fig, ax = plt.subplots(1, 1, figsize=[14, 12])
fig.subplots_adjust(right=0.8)
scat = ax.scatter(df['life_expect'], df['fertility'],
                  s=df['pop'] / 100000, alpha=0.7, edgecolor="black", cmap="viridis")

plt.xlabel("X")
plt.ylabel("Y")
x = 1.1
y = 0.1
is_first = True
kw = dict(prop="sizes", num=6, color="lightgrey", markeredgecolor='black', markeredgewidth=2)
handles, labels = scat.legend_elements(**kw)

inverted_transData = ax.transData.inverted()
for handle, label in zip(handles[::-1], labels[::-1]):
    plt.setp(handle, clip_on=False)
    for _ in range(1 if is_first else 2):
        plt.setp(handle, transform=ax.transAxes)
        if is_first:
            xd, yd = x, y
        else:
            xd, yd = inverted_transData.transform((x, y))
        handle.set_xdata([xd])
        handle.set_ydata([yd])
        ax.add_artist(handle)
        bbox = handle.get_window_extent(fig.canvas.get_renderer())
        y += y - bbox.y0 + 15  # 15 pixels inbetween
    x = (bbox.x0 + bbox.x1) / 2
    if is_first:
        xd_text, _ = inverted_transData.transform((bbox.x1+10, y))
    ax.text(xd_text, yd, label, transform=ax.transAxes, ha='left', va='center')
    y = bbox.y1
    is_first = False
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    • 1970-01-01
    相关资源
    最近更新 更多