【问题标题】:python: scatter plot with median and CIpython:带有中位数和CI的散点图
【发布时间】:2012-09-17 19:06:46
【问题描述】:

【问题讨论】:

  • 到目前为止你有什么收获?您是否浏览过 matplotlib 库:matplotlib.org/gallery.html
  • i47.tinypic.com/72vked.png 粗线是中位数,虚线是 10 和 90 个百分位数。问题:线条不流畅。图形不如 R-plot 漂亮
  • 好的,我明白你的意思了。看起来 R 插值很多,因为链接图中没有太多的数据点也保证了如此高分辨率的“水彩”背景。我不认为 matplotlib 有这样的东西(看起来它在 R 中也很新),但也许你可以把你的代码放在这里来生成你的情节,人们可以对此发表评论,以提高分辨率或使其平滑填充等高线图。

标签: python median scatter-plot


【解决方案1】:

这给出了标准偏差带的等价物:

# generate random variables
x,y = generate_random()

# bin the values and determine the envelopes
df = bin_by(x, y, nbins=25, bins = None)

###
# Plot 1
###
# determine the colors
cols = ['#EE7550', '#F19463', '#F6B176']

with plt.style.context('fivethirtyeight'): 
    # plot the 3rd stdv
    plt.fill_between(df.x, df['5th'], df['95th'], alpha=0.7,color = cols[2])
    plt.fill_between(df.x, df['10th'], df['90th'], alpha=0.7,color = cols[1])
    plt.fill_between(df.x, df['25th'], df['75th'], alpha=0.7,color = cols[0])
    # plt the line
    plt.plot(df.x, df['median'], color = '1', alpha = 0.7, linewidth = 1)
    # plot the points
    plt.scatter(x, y, facecolors='white', edgecolors='0', s = 5, lw = 0.7)

plt.savefig('fig1.png', facecolor='white', edgecolor='none')
plt.show()


def bin_by(x, y, nbins=30, bins = None):
    """
    Divide the x axis into sections and return groups of y based on its x value
    """
    if bins is None:
        bins = np.linspace(x.min(), x.max(), nbins)

    bin_space = (bins[-1] - bins[0])/(len(bins)-1)/2

    indicies = np.digitize(x, bins + bin_space)

从我的blog 进行讨论并链接到我的 Github

【讨论】:

    【解决方案2】:

    从我的大段代码中剪切粘贴。它没有给我想要的东西。我根据 Evert 的建议发布

        fig = plt.figure(figsize=(8, 8))
        plt.plot(xlist, ylist, 'b,')
        plt.plot([0.0,0.8],[0.0,0.8],'y-')
        data2d=zip(xlist,ylist)
        bins = np.linspace(0.0, 0.2, 21)
        medianlist=binpercentile(data2d,bins)
        c10list=binpercentile(data2d,bins,0.1)
        c90list=binpercentile(data2d,bins,0.9)    
        centerbins=[(x+y)/2.0 for x,y in zip(bins[:-1],bins[1:])]
        centerbins.insert(0,0)
        medianlist.insert(0,0)
        c10list.insert(0,0)
        c90list.insert(0,0)
        plt.plot(centerbins,c10list,'r--')
        plt.plot(centerbins,c90list,'r--')
        plt.plot(centerbins,medianlist,'r-')
        imagefilename='%s.%s'%('.'.join(infile.split('.')[0:-1]),'diffmed.pdf')
        plt.savefig(imagefilename)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-17
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多