具有相同标签的不同散点图标记 [英] Different scatterplot markers with the same label
问题描述
我遇到与> Matplotlib,图例类似的问题带有一个标签的多个不同标记.感谢这个问题,我得以实现以下目标:组合两个Pyplot补丁用于传说.
I am having 'similar' issues to Matplotlib, legend with multiple different markers with one label. I was able to achieve the following thanks to this question Combine two Pyplot patches for legend.
fig = pylab.figure()
figlegend = pylab.figure(figsize=(3,2))
ax = fig.add_subplot(111)
point1 = ax.scatter(range(3), range(1,4), 250, marker=ur'$\u2640$', label = 'S', edgecolor = 'green')
point2 = ax.scatter(range(3), range(2,5), 250, marker=ur'$\u2640$', label = 'I', edgecolor = 'red')
point3 = ax.scatter(range(1,4), range(3), 250, marker=ur'$\u2642$', label = 'S', edgecolor = 'green')
point4 = ax.scatter(range(2,5), range(3), 250, marker=ur'$\u2642$', label = 'I', edgecolor = 'red')
figlegend.legend(((point1, point3), (point2, point4)), ('S','I'), 'center', scatterpoints = 1, handlelength = 1)
figlegend.show()
pylab.show()
但是,我的两个(金星和火星)标记在图例中重叠.我尝试使用handlelength玩,但这似乎无济于事.任何建议或评论都将有所帮助.
However, my two (venus and mars) markers overlap in the legend. I tried playing with handlelength, but that doesn't seem to help. Any suggestions or comments would be helpful.
推荐答案
这是我的变通方法MWE.我实际上绘制了两个额外的图形",分别是point_g
和point_r
,它们具有我们将要使用的图例句柄.然后,我使用白色方形标记将其遮盖.根据需要绘制其余图.
Here's my work-around MWE. I actually plot two extra "plots", point_g
and point_r
which have the legend handles we will use. I then cover them up by using a white squre marker. Plot the remaining plots as desired.
import matplotlib.pyplot as plt
plt.rc('text', usetex=True)
plt.rc('text', **{'latex.preamble': '\\usepackage{wasysym}'})
plt.rc('lines', **{'markersize':20})
fig = plt.figure()
point_g, = plt.plot((0,), (0,), ls='none', marker='$\\male\\female$', mec='g')
point_r, = plt.plot((0,), (0,), ls='none', marker='$\\male\\female$', mec='r')
plt.plot((0,), (0,), marker='s', mec='w', mfc='w')
plt.plot(range(3), range(1,4), ls='none', marker='$\\male$', mec='g')
plt.plot(range(3), range(2,5), ls='none', marker='$\\male$', mec='r')
plt.plot(range(1,4), range(3), ls='none', marker='$\\female$', mec='g')
plt.plot(range(2,5), range(3), ls='none', marker='$\\female$', mec='r')
plt.axis([-0.1, 4.1, -0.1, 4.1])
plt.legend((point_g, point_r), ('Green', 'Red'), markerscale=1.6, numpoints=1,
borderpad=0.8, handlelength=3, labelspacing=1)
plt.show()
注意:如果您使用unicode符号,则不需要LaTeX前导.我无法让它们在我的系统(Linux)上运行,所以我使用了LaTeX符号.此方法适用于所有符号,只需删除plt.rc
命令并将\\male
和\\female
更改为您的Unicode字符即可.
Note: You do not need the LaTeX preamble if you use unicode symbols. I couldn't get them working on my system (Linux) so I used the LaTeX symbols. This method will work with all symbols, just remove the plt.rc
commands and change \\male
and \\female
to your unicode characters.
这篇关于具有相同标签的不同散点图标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!