Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why plt.show() shows one extra blank figure

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!

like image 555
thebeancounter Avatar asked Apr 05 '17 16:04

thebeancounter


People also ask

What does PLT show () do?

plt. show() starts an event loop, looks for all currently active figure objects, and opens one or more interactive windows that display your figure or figures. The plt. show() command does a lot under the hood, as it must interact with your system's interactive graphical backend.

Why is PLT plot blank?

The reason your plot is blank is that matplotlib didn't auto-adjust the axis according to the range of your patches. Usually, it will do the auto-adjust jobs with some main plot functions, such as plt. plot(), plt.

Is PLT show () blocking?

Answer #6: plt. show() and plt. draw() are unnecessary and / or blocking in one way or the other.

What is fig PLT figure ()?

plt. figure(figsize=(12,8)) creates an empty plot of the given size. plt. subplots(2, 2) creates a second plot with 4 subplots, but again with the default size. To create a plot with subplots and some given size, fig, ax_lst = plt.


1 Answers

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()
like image 128
Suever Avatar answered Oct 13 '22 14:10

Suever