如何制作仅高度指示器不显示完整条形图的条形图?

时间:2019-02-22 12:32:24

标签: python matplotlib plot bar-chart

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

目前,我使用'$-$'标记对事物进行了修补,这些标记使图例未对齐,并且无法正确放置。如果我更改无花果大小,则标记会从垂直条的x点(例如SDR)开始未对齐。

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

enter image description here

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

2 个答案:

答案 0 :(得分: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()

enter image description here

答案 1 :(得分:0)

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

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)

enter image description here