python - Matplotlib,图例符号之间的垂直空间

标签 python pandas matplotlib legend

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

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()
enter image description here

最佳答案

这有点棘手,但您可以测量图例元素并重新定位它们以具有恒定的中间距离。由于像素定位,此后无法调整绘图大小。
我使用“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()
resulting plot

关于python - Matplotlib,图例符号之间的垂直空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63533664/

相关文章:

python - 在 PyPlot 背景图像的范围内剪辑 Seaborn Kdeplot

python - 将数据帧值拆分为指定数量的组并应用函数 - pandas

python - 标准化已用 np.nan 填充的 3D NumPy 数组

python - Pandas:如何使用 df.to_dict() 轻松共享示例数据框?

python - 是否可以测试图例是否覆盖了 matplotlib/pyplot 中的任何数据

python - Matplotlib - 箱线图腿是最小值和最大值

javascript - 如何从 javascript 运行 python 脚本?

python - 将 2 个给定日期之间的天数拆分为特定的批量大小,并相应地获取开始日期和结束日期

python - Pandas:astype错误字符串到 float (无法将字符串转换为 float : '7,50')

python - Pandas Dataframe 具有各种列标准的求和函数