【问题标题】:How to make a bar chart with only a height indicator not showing the full bar?如何制作只有高度指示器而不显示完整条形的条形图?
【发布时间】:2019-02-22 12:32:24
【问题描述】:

这个图表看起来不错,但可能不是在 matplotlib 中建模的方法。如何在 x 点处有两个水平条在垂直线的左右延伸,以显示两个数据集的变化,例如 SDR 从 0.7 到 0.25。

目前我用'$-$' 标记拼凑一些东西,这些标记使图例不对齐,我无法正确放置。如果我更改 figsize,标记开始从 x 点处的垂直条错位,例如 SDR。

如何对这种图表进行探针建模?

layer0 = np.random.random(10)

fig, ax = plt.subplots(1,1, figsize=(15/2,1.5*2.5),)
ind = np.arange(10, dtype=np.float64)*1#cordx

ax.plot(ind[0::2]+0.05, layer0[0::2]-0.04, ls='None', marker='$-$', markersize=40)
ax.plot(ind[1::2]-0.15, layer0[1::2]-0.04, ls='None', marker='$-$', markersize=40)
ax.set_ylim(0,1.05) 
ax.set_yticks(np.arange(0, 1.1, step=0.1))
ax.set_xticks(ind[0::2]+0.5)
ax.set_xticklabels( ('SDR', 'SSR', 'SCR', 'RCR', 'GUR') )
plt.grid(b=True)
plt.grid(color='black', which='major', axis='y', linestyle='--', lw=0.2)
plt.show()

【问题讨论】:

    标签: python matplotlib plot bar-chart


    【解决方案1】:

    或者,您可以使用水平条形图barh,在这种情况下更直观。这里的关键参数是left,它将您的水平条形图向左/向右移动。

    以下是完整的答案:

    import numpy as np
    import matplotlib.pyplot as plt
    
    np.random.seed(2)
    layer0 = np.random.random(10)
    
    fig, ax = plt.subplots(1,1, figsize=(15/2,1.5*2.5),)
    n = 10
    width = 0.5
    ind = np.arange(n, dtype=np.float64)*1#cordx
    
    ax.barh(layer0[0::2], [width]*int(n/2), height=0.01, left = ind[0::2])
    ax.barh(layer0[1::2], [width]*int(n/2), height=0.01, left = ind[0::2]+width)
    ax.set_ylim(0,1.05) 
    ax.set_yticks(np.arange(0, 1.1, step=0.1))
    ax.set_xticks(ind[0::2]+0.5)
    ax.set_xticklabels( ('SDR', 'SSR', 'SCR', 'RCR', 'GUR') )
    plt.grid(b=True)
    plt.grid(color='black', which='major', axis='y', linestyle='--', lw=0.2)
    plt.show()
    

    【讨论】:

      【解决方案2】:

      到目前为止,我还没有想到带有底部偏移的条形图,这似乎还可以:

      layer0 = np.random.random(10)
      fig, ax = plt.subplots(1,1, figsize=(15/1.3,1.5*2.5),)# sharey=True)
      ind = np.arange(10, dtype=np.float64)*1#cordx
      height=0.03
      width=0.8
      ax.bar(ind[0::2]-width/2, height, width=width, bottom=layer0[0::2]-height)
      ax.bar(ind[0::2]+width/2, height, width=width, bottom=layer0[1::2]-height)
      ax.set_ylim(-0.,1.05)
      plt.grid(color='black', which='major', axis='x', linestyle='-', lw=0.8)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-31
        • 1970-01-01
        • 1970-01-01
        • 2015-09-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多