【问题标题】:How to set markeredgecolor to a color map based off of a pandas DataFrame如何将 markeredgecolor 设置为基于 pandas DataFrame 的颜色图
【发布时间】:2020-03-31 17:01:23
【问题描述】:

我想使用来自 DataFrame 的两组不同数据制作散点图,其中一组具有实心圆形标记,另一组带有空心圆形标记,但两者都由 DataFrame 中的另一列进行颜色编码。我有这个情节的代码是:

    plt.figure(figsize = (10,10))
    plt.rcParams.update({'font.size': 30})
    plt.errorbar(fullmergedf['ir_SFR-UV_corr'] , fullmergedf['PAB_L'] , yerr = fullmergedf['PAB_L_ERR']  , linestyle = 'None' , c = 'grey' )
    plt.scatter(fullmergedf['ir_SFR-UV_corr'] , fullmergedf['PAB_L'] , s = 200 , c = fullmergedf['td_lmass'], cmap = 'coolwarm')
    plt.scatter(fullmergedf['ir_SFR-ladder_total'] , fullmergedf['PAB_L'] , s = 200 , c = fullmergedf['td_lmass'], cmap = 'coolwarm' , markerfacecolor = 'none')
    cb = plt.colorbar()
    cb.set_label('Log$[M_{\odot}]$')
    plt.ylabel("PaB L [erg/s]")
    plt.xlabel("UV + IR Ladder-SFR [$M_{\odot}/yr$]")
    plt.axis([min(fullmergedf['ir_SFR-ladder_total']) -10**-6 , max(fullmergedf['ir_SFR-ladder_total']) + 10**2 , min(fullmergedf['PAB_L']) - 10**36, max(fullmergedf['PAB_L'])  + 10**41])
    plt.xscale('log')
    plt.yscale('log')
    plt.show()

我尝试将第二个plt.scatter() 中的c = fullmergedf['td_lmass'] 更改为markeredgecolor = fullmergedf['td_lmass'],但这不起作用。我所见过的解决方案都没有用彩条做空心标记。

【问题讨论】:

    标签: python matplotlib astronomy


    【解决方案1】:

    您可以先按正常方式创建散点图,然后将 facecolor 设置为 'none':

    import numpy as np
    import matplotlib.pyplot as plt
    
    plt.scatter(np.random.rand(10), np.random.rand(10), s=200, c=np.linspace(0, 1, 10), cmap='Reds')
    scatterdots = plt.scatter(np.random.rand(10), np.random.rand(10), s=200, c=np.linspace(0, 1, 10), cmap='Greens', lw=3)
    scatterdots.set_facecolor('none')
    plt.show()
    

    在问题的代码中,它会是这样的:

    scatterdots = plt.scatter(fullmergedf['ir_SFR-ladder_total'], fullmergedf['PAB_L'], s=200, c=fullmergedf['td_lmass'], cmap='coolwarm')
    scatterdots.set_facecolor('none')    
    

    【讨论】:

    • 这行得通,谢谢。在plt.scatter() 的论点中说markerfacecolor = 'none' 不起作用是有原因的吗?
    • cmarkerfacecolor都设置时,设置c=得到的颜色优先
    猜你喜欢
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 2020-02-26
    • 2016-02-06
    • 1970-01-01
    • 2021-10-28
    • 2012-03-21
    • 1970-01-01
    相关资源
    最近更新 更多