2

我正在绘制一个没有轴的条形图。我只想显示具有非零值的条形图。如果它是零,我根本不想要酒吧。目前它会在零轴上显示一条细线,我希望它消失。我怎样才能做到这一点?

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

data = (0, 1890,865, 236, 6, 1, 2, 0 , 0, 0, 0 ,0 ,0 ,0, 0, 0)
ind = range(len(data))
width = 0.9   # the width of the bars: can also be len(x) sequence

p1 = plt.bar(ind, data, width)
plt.xlabel('Duration 2^x')
plt.ylabel('Count')
plt.title('DBFSwrite')
plt.axis([0, len(data), -1, max(data)])

ax = plt.gca()

ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)

plt.savefig('myfig')

样本输出

看到 x=0 和 x=7-16 处的细线了吗?我想消除那些。

4

1 回答 1

4

您可以使用numpy的数组,并创建一个掩码,您可以使用它来过滤掉data值为 0 的索引。

import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

data = np.array([0, 1890,865, 236, 6, 1, 2, 0 , 0, 0, 0 ,0 ,0 ,0, 0, 0])
ind = np.arange(len(data))
width = 0.9   # the width of the bars: can also be len(x) sequence

mask = data.nonzero()

p1 = plt.bar(ind[mask], data[mask], width)
plt.xlabel('Duration 2^x')
plt.ylabel('Count')
plt.title('DBFSwrite')
plt.axis([0, len(data), -1, max(data)])

ax = plt.gca()

ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)

plt.savefig('myfig')

在此处输入图像描述

于 2013-08-06T19:01:53.217 回答