gpt4 book ai didi

python - Matplotlib:如何调整图例标记和标签之间的间距?

转载 作者:太空狗 更新时间:2023-10-29 17:16:30 24 4
gpt4 key购买 nike

我想调整图例标记和标签之间的间距。有时默认空间太大。有谁知道如何做到这一点?

谢谢。

最佳答案

legend()在名为 handletextpad 中有一个 kwarg,它将满足您的需求。默认情况下,该值设置为 0.8。来自docs :

handletextpad : float or None

The pad between the legend handle and text. Measured in font-size units.

Default is None which will take the value from the legend.handletextpad rcParam.

因此,当您调用 legend 时,添加该 kwarg,并试验该值。像这样的东西:

ax.legend(handletextpad=0.1)

考虑以下几点:

import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(ncols=2)

ax1.plot(range(5), 'ro', label='handletextpad=0.8')
ax2.plot(range(5), 'bo', label='handletextpad=0.1')

ax1.legend()
ax2.legend(handletextpad=0.1)

plt.show()

enter image description here

关于python - Matplotlib:如何调整图例标记和标签之间的间距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41827406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com