matplotlib图例中的两个相邻符号

2024-06-01 04:51:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我想确定两个不同的符号(不同的颜色)在同一行在一个图例。下面,我试着用代理艺术家来做这个,但结果是他们在图例中相互堆叠。我想让它们挨着一个或一个在另一个上面,这样它们都是可见的。在

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, legend with multiple different markers with one label),但似乎这个问题还没有解决,因为那里的答案只是在另一个补丁上画了一个补丁,这也正是我遇到的问题。我觉得也许我需要做一个复合补丁,但我很难找到如何做到这一点。谢谢!在

此外,我还没有找到如何使图例符号看起来与散点符号相同(线条粗细、大小)。再次感谢。在


Tags: none符号markercolor图例legendax2line2d
1条回答
网友
1楼 · 发布于 2024-06-01 04:51:42

对不起,我不回答你的主要问题。 但是,关于你的最后一点

how to make the legend symbols look the same (line thickness, size) as the scatter symbols

您可以使用legend命令的关键字markerscale。同样大小的

    ax2.legend( ...<handles_and_labels>...  , markerscale=1)

或者在rcParams中改变legend.markerscale。在

相关问题 更多 >