簡體   English   中英

matplotlib 混合散點和線 plot 中未顯示的圖例標記

[英]Legend markers not shown in matplotlib mixed scatter and line plot

我有一個帶有圖例的 plot,看起來像在此處輸入圖像描述

它是使用以下代碼創建的

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$) 檢測”或“生存分析”的標記。 我懷疑這與我將這兩個散點圖的面部顏色設置為無有關。 我如何得到這個,以便它分別顯示一個空心菱形和空心圓?

您只需設置c顏色( ec )而不是coloredgecolor ):

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()

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM