![](/img/trans.png)
[英]how to remove empty space from bars for a specific group, that was plotted as seaborn bar plot on data in pandas dataframe
[英]Remove empty space from matplotlib bar plot
我将两个条形图绘制到同一个轴上,其中 x 轴包含从 0 向下的值。 但是,我的数据中有很大的差距(例如从 0 到 -15),并且想要删除空白空间,以便轴从 0 到 -15 之间没有空格 - 显示在此图像上:
理想情况下,我想为所有差距做到这一点。 我已经尝试过plt.axis('tight')
和fig.tight_layout()
,但它们都没有奏效。
编辑:一个小例子的示例代码
keys = [0, -15, -16, -17]
values = [3, 5, 2, 1]
fig, ax = plt.subplots(ncols=1)
fig.tight_layout()
ax.bar(keys, values, 0.8, color='g', align='center')
ax.set_xticks(keys)
plt.setp(ax.xaxis.get_majorticklabels(), rotation=90 )
您将需要创建一个断轴。
请参阅文档: https : //matplotlib.org/3.1.0/gallery/subplots_axes_and_figures/broken_axis.html
x
绘制values
,该x
是对应于keys
len
的范围,然后更改xticklabels
。import matplotlib.pyplot as plt
keys = [0, -15, -16, -17]
values = [3, 5, 2, 1]
fig, ax = plt.subplots()
# create the xticks locations
x = range(len(keys))
ax.bar(x, values, 0.8, color='g', align='center')
# set the ticks and labels
ax.set_xticks(x)
_ = ax.set_xticklabels(keys)
keys = [0, -15, -16, -17]
values = [3, 5, 2, 1]
# zip, sort and unpack
keys, values = zip(*sorted(zip(keys, values)))
fig, ax = plt.subplots()
# create the xticks locations
x = range(len(keys))
ax.bar(x, values, 0.8, color='g', align='center')
# set the ticks and labels
ax.set_xticks(x)
_ = ax.set_xticklabels(keys)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.