Why does plt.show () show one extra blank digit

I am using python 2.7 and am trying to plot a simple volatile percent chart.

I am getting the digit I want, the problem is when using plt.show () I am getting 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()

      

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

Thank!

+3


source to share


1 answer


The problem is what plt.grid(True)

works with the current drawing, and since there is no shape now, when you get to that line, it is created automatically. Then you create another shape when you callplt.subplots()

After creating the graphs, you must add grid lines

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

plt.show()

      



Alternatively, just call bar

without calling subplots

, as it bar

will automatically create the shape and axes as needed.

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

      

+4


source







All Articles