Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib plots turn out blank

I am trying to plot circles in matplotlib, but the outcome is always an empty plot.

E.g.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.collections as mcollections

fig = plt.figure()
fig.set_size_inches(18.5, 10.5, forward=True)
ax = fig.add_subplot(111, aspect='equal')

x = np.array([17., 29., 41.,  3., 15.])
y = np.array([21., 41., 30., 19., 5.])
r = np.array([22.8035085, 46.04345773, 46.61544808, 16.,  12.16552506])

patches = [mpatches.Circle((xx, yy), rr) for xx, yy, rr in zip(x, y, r)]
collection = mcollections.PatchCollection(patches)
ax.add_collection(collection) 

fig.savefig("test.png")

This produces an empty plot, the same when I try with add_artist. Hopefully someone can point me to where I am going wrong! Thank you

like image 962
erocoar Avatar asked Apr 20 '17 00:04

erocoar


People also ask

Why is my plot not showing up in Python?

It means if we are not using the show() function, it wouldn't show any plot. When we use the show() function in the non-interactive mode. That means when we write the code in the file it will show all the figures or plots and blocks until the plots have been closed.

What happens if I dont use %Matplotlib inline?

In the current versions of the IPython notebook and jupyter notebook, it is not necessary to use the %matplotlib inline function. As, whether you call matplotlib. pyplot. show() function or not, the graph output will be displayed in any case.

How do I use Savefig in matplotlib?

Saving a plot on your disk as an image file Now if you want to save matplotlib figures as image files programmatically, then all you need is matplotlib. pyplot. savefig() function. Simply pass the desired filename (and even location) and the figure will be stored on your disk.

Why is my Matplotlib 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.scatter () .... As far as I'm concerned, it's not designed for drawing geometric figures like triangles, rectangle and circles.

How to clear the figure plot in Matplotlib?

The title of the figure is ‘matplotlib.figure.Figure.clear () function Example’. The gridlines are also plotted for the figure by setting ax.grid (True). But before the plt.show () statement that shows the plotted figure, we use the fig.clear () function. The fig.clear () function clears the figure plot when ‘True’ is an argument.

What is Matplotlib in Python?

Matplotlib is a library in Python, which is a numerical – mathematical extension for NumPy library. The figure module of the Matplotlib library provides the top-level Artist, the Figure, which contains all the plot elements. The figure module is used to control the subplots’ default spacing and top-level container for all plot elements.

What is the use of keep_observers in Matplotlib?

The ‘keep_observers’ parameter in figure.clear() function is a boolean value. Return type The clear() function does not return any value. It is used in python programs to clear plots. Example of Matplotlib Figure clear plot


2 Answers

In addition to the other answer provided, you can use ax.autoscale() before saving the plot. that will result in

enter image description here

like image 125
plasmon360 Avatar answered Nov 03 '22 00:11

plasmon360


Your problem here is not the paches haven't been drawed. 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.scatter() .... As far as I'm concerned, it's not designed for drawing geometric figures like triangles, rectangle and circles. This may also explain why this called patches in matplotlib.

So what you need to do is to specify the range of the axis manully by using something like this in your case:

plt.axis([-50, 100, -50, 100])
like image 39
Feishi Avatar answered Nov 03 '22 00:11

Feishi