11
votes

I use python 2.7 and trying to plot a simple percentile bat chart.

I get the figure that i want, the problem is that, with it, when using plt.show() i get an extra blank image,

I tried plt.close(), plt.clf() and plt.figure() to create a clean plt object, this is my function:

import matplotlib.pyplot as plt
plt.grid(True)
data = zip(*percentiles)

data = [list(i) for i in data]
tick_range = data[0]

ticks = [str(i) + "%" for i in tick_range]
tick_range = [x+2.5 for x in tick_range]

fig, ax = plt.subplots()
plt.bar(data[0], data[1], width=5)

plt.show()

the data (percentiles) variable is of the following structure [(i,v),(i,v)....] when i is a index, and v is a floating point value.

Thanks!

1

1 Answers

9
votes

The issue is that plt.grid(True) operates on the current figure and since no figure currently exists when you get to that line, one is created automatically. Then you create another figure when you call plt.subplots()

You should add the gridlines after you create your plots

plt.bar(data[0], data[1], width=5)
plt.grid(True)

plt.show()

Alternately, just call bar without calling subplots since bar will automatically create a figure and axes as needed.

plt.grid(True)
plt.bar(data[0], data[1], width=5)
plt.show()