我用Python制作了一个水平条形图,条形图从左到右排列:
我想添加一个额外的水平条,在上一个条的同一水平面上,这次是从右到左。两个单杠应该同时出现。
有谁知道怎么做吗?如果我使用reverse函数,所有内容都会被反转,但我只需要反转新的特定条,而不需要更改其他任何内容。
理想情况下,在新图片上,新的条形图将从右侧开始,并在25处停止,误差条数从23到27 (-/+ 2)。
下面是我的脚本:
import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize=(9.5, 2.7))
# Create horizontal bars
plt.barh(0, 18,height=0.2,facecolor='orange',edgecolor='black',linewidth=2)
plt.errorbar(x=[18], y=[0], xerr=[2],color='black',fmt='none',linewidth=5,zorder=4)
plt.xticks(np.arange(10, 30+1, 1.0),fontsize=14)
plt.yticks([])
plt.xlim(10, 30)
plt.ylim(-.13, .13)
plt.show()
发布于 2019-03-02 06:38:48
诀窍是使用left
指定条形图应该从哪里开始,然后为条形图传递一个负的width
,使其从右向左延伸。由于窗口的右侧也会随着数据的变化而变化,因此您可能还希望将其设置为某种类型的参数,即x_max
import numpy as np
import matplotlib.pyplot as plt
x_max = 30
plt.figure(figsize=(9.5, 2.7))
# Create horizontal bars
plt.barh(0, 18,height=0.2,facecolor='orange',edgecolor='black',linewidth=2)
plt.errorbar(x=[18], y=[0], xerr=[2],color='black',fmt='none',linewidth=5,zorder=4)
# new code: use left to specify the start position, then make its width negative
# to extend right to left
plt.barh(0, -5, height=0.2, left=x_max, facecolor='red',edgecolor='black',linewidth=2)
# place error bars the same as you did for the above.
plt.errorbar(x=[x_max - 5], y=[0], xerr=[2],color='black',fmt='none',linewidth=5,zorder=4)
plt.xticks(np.arange(10, 30+1, 1.0),fontsize=14)
plt.yticks([])
plt.xlim(10, x_max)
plt.ylim(-.13, .13)
plt.show()
https://stackoverflow.com/questions/54952358
复制相似问题