【问题标题】:Errorbar variable marker size误差条可变标记大小
【发布时间】:2019-09-25 14:27:25
【问题描述】:

我想绘制一些数据xy,其中我需要标记大小取决于第三个数组z。我可以分别绘制它们(即,用size = z 分散xy,以及没有标记的错误栏fmc = 'none'),这解决了它。问题是我需要图例一起显示错误栏和点:

而不是

代码在这里带有一些虚构的数据:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(1,10,100)
y = 2*x
yerr = np.random(0.5,1.0,100)
z = np.random(1,10,100)

fig, ax = plt.subplots()

plt.scatter(x, y, s=z, facecolors='', edgecolors='red', label='Scatter') 
ax.errorbar(x, y, yerr=yerr, xerr=0, fmt='none', mfc='o', color='red', capthick=1, label='Error bar')

plt.legend()

plt.show()

这会产生我想避免的图例:

errorbar the argumentmarkersizedoes not accept arrays asscatter` 中。

【问题讨论】:

    标签: matplotlib errorbar


    【解决方案1】:

    这个想法通常是使用代理来放入图例。因此,虽然图中的错误栏可能没有标记,但图例中的错误栏有一个标记集。

    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(1,10,11)
    y = 2*x
    yerr = np.random.rand(11)*5
    z = np.random.rand(11)*2+5
    
    fig, ax = plt.subplots()
    
    sc = ax.scatter(x, y, s=z**2, facecolors='', edgecolors='red') 
    errb = ax.errorbar(x, y, yerr=yerr, xerr=0, fmt='none', 
                       color='red', capthick=1, label="errorbar")
    
    proxy = ax.errorbar([], [], yerr=[], xerr=[], marker='o', mfc="none", mec="red", 
                        color='red', capthick=1, label="errorbar")
    ax.legend(handles=[proxy], labels=["errorbar"])
    plt.show()
    

    【讨论】:

    • 这解决了我的问题。感谢您快速明确的回答。
    猜你喜欢
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2013-01-27
    相关资源
    最近更新 更多