gpt4 book ai didi

python - 当标记设置为像素时如何更改图例中的标记大小/比例

转载 作者:行者123 更新时间:2023-11-28 21:33:40 26 4
gpt4 key购买 nike

我用一个非常小的标记散点图数据点(见下面的屏幕截图)。当我使用非常小的标记 ',' 时,图例很难阅读(示例代码取自 here )。
(Python 3,Jupyter 实验室)

enter image description here

如何增加图例中标记的大小。上述网站上显示的两个版本均无效:

legend = ax.legend(frameon=True)  
for legend_handle in legend.legendHandles:
legend_handle._legmarker.set_markersize(9)

ax.legend(markerscale=6)

当标记设置为“.”时,这两个解决方案确实有效。
如何在图例中展示更大的制造商?

示例代码来自 intoli.com :

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(12)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

for i in range(5):
mean = [np.random.random()*10, np.random.random()*10]
covariance = [ [1 + np.random.random(), np.random.random() - 1], [0, 1 + np.random.random()], ]
covariance[1][0] = covariance[0][1] # must be symmetric
x, y = np.random.multivariate_normal(mean, covariance, 3000).T
plt.plot(x, y, ',', label=f'Cluster {i + 1}')

ax.legend(markerscale=12)

fig.tight_layout()
plt.show()

最佳答案

通过将标记大小设置为 1 像素,您可以为 绘图 获得 1 像素大小的标记。这看起来像

plt.plot(x, y, marker='s', markersize=72./fig.dpi, mec="None", ls="None")

上面所做的是将标记设置为正方形,将标记大小设置为 ppi(每英寸点数)除以 dpi(每英寸点数)== 点数 == 像素,并删除线条和边缘。

然后您尝试在图例中使用 markerscale 的解决方案效果很好。

完整示例:

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(12)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

for i in range(5):
mean = [np.random.random()*10, np.random.random()*10]
covariance = [ [1 + np.random.random(), np.random.random() - 1], [0, 1 + np.random.random()], ]
covariance[1][0] = covariance[0][1] # must be symmetric
x, y = np.random.multivariate_normal(mean, covariance, 3000).T
plt.plot(x, y, marker='s', markersize=72./fig.dpi, mec="None", ls="None",
label=f'Cluster {i + 1}')

ax.legend(markerscale=12)

fig.tight_layout()
plt.show()

enter image description here

关于python - 当标记设置为像素时如何更改图例中的标记大小/比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54398090/

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