高度为零时matplotlib不可见条

高度为零时matplotlib不可见条,matplotlib,Matplotlib,我在画一个没有轴的条形图。我只想显示非零值的条形图。如果它是零,我不想要任何酒吧。现在它会在零轴上显示一条很小的线,我希望它消失。我该怎么做 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

我在画一个没有轴的条形图。我只想显示非零值的条形图。如果它是零,我不想要任何酒吧。现在它会在零轴上显示一条很小的线,我希望它消失。我该怎么做

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时的非常细的线了吗?我想消除这些。

您可以使用的数组,并创建一个掩码,您可以使用该掩码过滤掉
数据
值为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')

您应该包含到目前为止的代码!好主意,我添加了代码和示例输出。