[英]How to change marker size/scale in legend when marker is set to pixel
I am scatter ploting data points with a very small marker (see screengrab below). 我用一个很小的标记分散地绘制数据点(请参见下面的屏幕抓图)。 When I use the very small marker ',' the legend is very hard to read (example code taken from here ). 当我使用非常小的标志“”传说是非常难以阅读(取自示例代码在这里 )。
(Python 3, Jupyter lab) (Python 3,Jupyter实验室)
How can I increase the size of the marker in the legend. 如何增加图例中标记的大小。 The two versions shown on the above mentioned site do not work: 上述站点上显示的两个版本不起作用:
legend = ax.legend(frameon=True)
for legend_handle in legend.legendHandles:
legend_handle._legmarker.set_markersize(9)
and 和
ax.legend(markerscale=6)
The two solutions do however work when the marker is set to '.'. 但是,当标记设置为“。”时,这两种解决方案都可以使用。
How can I show bigger makers in the legend? 如何在传奇中展现更大的制造商?
Sample Code from intoli.com : 来自inli.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()
You can get 1 pixel sized markers for a plot
by setting the markersize to 1 pixel. 你可以得到一个1像素大小的标志plot
由markersize设置为1个像素。 This would look like 这看起来像
plt.plot(x, y, marker='s', markersize=72./fig.dpi, mec="None", ls="None")
What the above does is set the marker to a square, set the markersize to the ppi (points per inch) divided by dpi (dots per inch) == dots == pixels, and removes lines and edges. 上面的操作是将标记设置为正方形,将标记大小设置为ppi(每英寸点数)除以dpi(每英寸点数)==点数==像素,然后删除线条和边缘。
Then the solution you tried using markerscale
in the legend works nicely. 然后,您尝试在图例中使用markerscale
的解决方案将很好地工作。
Complete example: 完整的例子:
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()
According to this discussion , the markersize
has no effect when using pixels ( ,
) as marker. 根据本讨论中, markersize
当使用像素(没有效果,
)作为标记物。 How about generating a custom legend instead? 如何生成自定义图例呢? For example, by adapting the first example in this tutorial , one can get a pretty decent legend: 例如,通过改编本教程中的第一个示例,可以得到一个不错的图例:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
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}')
##generating custom legend
handles, labels = ax.get_legend_handles_labels()
patches = []
for handle, label in zip(handles, labels):
patches.append(mpatches.Patch(color=handle.get_color(), label=label))
legend = ax.legend(handles=patches)
fig.tight_layout()
plt.show()
The output would look like this: 输出如下所示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.