matplotlib图例中的两个相邻符号 [英] Two adjacent symbols in matplotlib legend
问题描述
我想在图例的同一行上标识两个不同的符号(具有不同的颜色).下面,我尝试与Proxy Artists一起执行此操作,但结果是它们在图例中彼此堆叠.我希望它们彼此相邻或彼此相邻-以便它们都可见.
I would like to identify two different symbols (with different colors) on the same line in a legend. Below, I tried doing this with Proxy Artists, but the result is that they get stacked on top of each other in the legend. I want them next to each other or one above the other-- so they are both visible.
from pylab import *
import matplotlib.lines as mlines
#define two colors, one for 'r' data, one for 'a' data
rcolor=[69./255 , 115./255, 50.8/255 ]
acolor=[202./255, 115./255, 50.8/255 ]
#Plot theory:
ax2.plot(rho, g_r, '-',color=rcolor,lw=2)
ax2.plot(rho, g_a, '-',color=acolor,lw=2)
#Plot experiment:
ax2.scatter(X_r, Y_r,s=200, marker='s', facecolors='none', edgecolors=rcolor);
ax2.scatter(X_a, Y_a,s=200, marker='^', facecolors='none', edgecolors=acolor);
#Create Proxy Artists for legend
expt_r = mlines.Line2D([], [], fillstyle='none', color=rcolor, marker='s', linestyle='', markersize=15)
expt_a = mlines.Line2D([], [], fillstyle='none', color=acolor, marker='^', linestyle='', markersize=15)
thry_r = mlines.Line2D([], [], fillstyle='none', color=rcolor, marker='', markersize=15)
thry_a = mlines.Line2D([], [], fillstyle='none', color=acolor, marker='', markersize=15)
#Add legend
ax2.legend(((expt_r,expt_a),(thry_r,thry_a)), ('Experiment','Theory'))
我认为我的问题几乎与此完全相同:( Matplotlib,带有多个带有一个标签的不同标记的图例),但问题似乎尚未解决,因为那里的答案只是在另一个补丁上绘制了一个补丁,这对我来说也正是如此.我觉得也许我需要以某种方式制作一个复合补丁,但是我很难找到方法.谢谢!
I think my problem is almost exactly like this one: (Matplotlib, legend with multiple different markers with one label), but it seems like the problem is unsolved since the answer there just plots one patch on top of the other, which is exactly what happens for me too. I feel like maybe I need to make a composite patch somehow, but I had trouble finding how to do this. Thanks!
此外,我还没有找到如何使图例符号看起来与散点符号相同(线条粗细,大小)的方法.再次感谢.
Also, I haven't found how to make the legend symbols look the same (line thickness, size) as the scatter symbols. Thanks again.
推荐答案
抱歉,我没有回答您的主要问题. 但是,关于您的最后一点
I do not answer your main question, sorry. However, regarding your last point
如何使图例符号看起来与散布符号相同(线宽,大小)
how to make the legend symbols look the same (line thickness, size) as the scatter symbols
您可以使用legend
命令的关键字markerscale
.所以对于相等的大小
you can use the keyword markerscale
of the legend
command. So for equal size
ax2.legend( ...<handles_and_labels>... , markerscale=1)
或在rcParams
中进行legend.markerscale
更改.
这篇关于matplotlib图例中的两个相邻符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!