图例标记未显示在matplotlib混合散点图和线图中

2024-05-16 21:45:55 发布

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

我有一个关于传说的情节 enter image description here

它是用以下代码创建的

fig = plt.figure(figsize = (30 , 11))
gs = GridSpec(nrows=12, ncols=15)
gs.update(wspace = 0, hspace = 0.5)

ax0 = fig.add_subplot(gs[2:12, 0:5])
#ax0.scatter(fullmergedf['td_lmass'] , fullmergedf['PAB_SFR_EX_LL_1SIGMA'] , s = 200 , marker = '+' , c = 'black')
#ax0.errorbar(fullmergedf['td_lmass'] , fullmergedf['PAB_SFR_EX2'] , yerr = fullmergedf['PAB_SFR_EX2_ERR'] , c = 'gray',  linestyle = 'None')
#ax0.scatter(fullcleardf['td_lmass'] , fullcleardf['PAB_SFR_EX2_LIMIT'] , s = 20, c = 'black')
y0 = ax0.scatter(fullmergedf['td_lmass'] , fullmergedf['PAB_SFR_EX2'] , s = 200 , c = fullmergedf['ir_UV_beta'] , cmap = 'coolwarm' , label = 'Cleri et al. 2020')
scatter0 = ax0.scatter(fullmergebaddf['td_lmass'] , fullmergebaddf['PAB_SFR_EX2'] , s = 200, c = fullmergebaddf['ir_UV_beta'] , cmap = 'coolwarm' , marker = 'D' , label = 'Marginal (1$\sigma$ - 3$\sigma$) Detections')
scatter0.set_facecolor('none')
ax0.scatter(sortedclearmergedf['td_lmass'] , sortedclearmergedf['PAB_SFR_EX2_LIMIT'] , s = 20, c = 'black' , marker = 'v' , label = '3D-HST 1$\sigma$ Limits')
scatterdots0 = ax0.scatter(sortedclearmergedf['td_lmass'] , sortedclearmergedf['PAB_SFR_EX2_LIMIT_RAND'] , s = 200, c = sortedclearmergedf['ir_UV_beta'] , cmap = 'coolwarm' , label = 'Survival Analysis')
scatterdots0.set_facecolor('none')

ax0.plot(limitdf['td_lmass'] , limitdf['ROLL_MAD'] , c = 'green' , label = 'Rolling MAD')
ax0.plot(limitdf['td_lmass'] , -limitdf['ROLL_MAD'] , c = 'green')
ax0.plot(limitdf['td_lmass'] , limitdf['ROLL_MAD_FIT_TOP'] , c = 'gray' , label = 'Rolling MAD to fit')
ax0.plot(limitdf['td_lmass'] , limitdf['ROLL_MAD_FIT_BOT'] , c = 'gray')
ax0.plot([6 , 11] , [0 , 0] , '--' , c = 'gray')
ax0.set_ylabel(r'log($SFR_{Pa\beta}$) - log($SFR_{UV}^{corr}$)')
ax0.set_xlabel(r"log$(M_*/M_{\odot})$")
ax0.axis([6.5 , 10.5 , -3 , 3])
for i in range(0, len(lmlimitall.chain), 25):
    xs = np.arange(-6,12)
    ys = lmlimitall.chain[i]['alpha'] + xs * lmlimitall.chain[i]['beta']
    ax0.plot(xs, ys, color='gray', alpha=0.03)
# ax1.plot([-2 , 2] , [np.mean(lmpabhabeta.chain['alpha']) + np.mean(lmpabhabeta.chain['beta'])*-2 , np.mean(lmpabhabeta.chain['alpha']) + np.mean(lmpabhabeta.chain['beta'])*2] , c = 'black' , label = 'm = ' + str(round(np.mean(lmpabhabeta.chain['beta']) , 2)) + r'$\pm$' + str(round(np.std(lmpabhabeta.chain['beta']), 2))  )

ax0.legend(bbox_to_anchor=(1.7, 0.5) , loc = 'center right')

ax3 = fig.add_subplot(gs[0:1,1:4])
fig.colorbar(y0,ax3,use_gridspec=True,orientation='horizontal' ,  label = r'$\beta$')
ax3.xaxis.set_label_position('top')
ax3.xaxis.get_label().set_verticalalignment('bottom')

plt.show()

右边的图例没有显示“边际(1$\sigma$-3$\sigma$)检测”或“生存分析”的标记。我怀疑这与我将这两个散点图的面部颜色设置为“无”有关。我如何得到它,使它分别显示一个空心钻石和空心圆


Tags: chainplotnplabelbetatdsetsfr
1条回答
网友
1楼 · 发布于 2024-05-16 21:45:55

您只需设置edgecolorec)而不是colorc):

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm


data = np.random.randint(0, 10, (2, 10))
cmap = cm.get_cmap('coolwarm')
colors = cmap(np.linspace(0, 1, 10))
fig, ax = plt.subplots()

scatter = ax.scatter(
    *data, s=10, ec=colors, marker='D', fc='none',
    label='Marginal (1$\sigma$ - 3$\sigma$) Detections'
)
ax.legend()
fig.show()

enter image description here

相关问题 更多 >