Python: equal axes plots with inline graphics? - python

I have just installed anaconda on a machine running Windows 7 Enterprise, 64 bit, and I'm using the ipython console within Spyder, which supports inline graphics. I am trying to plot a sequence of points with equal axes. According to the documentation, something like this should work:
import pylab
pylab.axes().set_aspect('equal')
pylab.plot(b[:,0],b[:,1],'.')
pylab.show()
However, any call to pylab immediately results in the creation of a figure, so with the above commands the first pylab call creates an empty figure (but with equal axes), and the second pylab call creates a new figure of the plot - but with unequal axes. How can I get both pylab calls to refer to the same figure so that I end up with one figure containing the plot and with equal axes?

In general pylab is not really recommended. It is better to import pyplot directly. Does this work for you?
from matplotlib import pyplot as plt
import numpy as np
b = np.arange(20.)
b.shape = (10, 2)
b[:, 1] *= 0.2
ax = plt.subplot(1,1,1)
ax.plot(b[:,0], b[:,1], '.')
ax.set_aspect('equal')
Setting the aspect ratio is generally one of the last operations I perform on an axes because other plotting operations often messes it up. Also note here that I have followed the 'use pyplot to create the figures and then the OO interface for plotting' guidance on this page.

Related

How to apply an option to several figures at the same time

If I do this
import numpy as np
import matplotlib.pyplot as plt
a=[1,2,3]
b=[3,4,5]
plt.figure(1)
plt.xlim(0,3)
plt.plot(b)
plt.figure(2)
plt.plot(a)
plt.show()
the choice of the x axes will be applied only to figure 1. What can I use to discriminate between the options that I want to be valid for only figure 1 or 2 and the ones that I want to be applied to both figures?
Clarification: I know that it is possible to call plt.xlim several times. I was rather looking for some command of a form like
plt.apply_options_to(1,2)
and from that moment on every time I call plt.xlim the option is applied to both figures and not only one of the two.
With pyplot, each command applies to the currently active figure or axes. This means you can easily loop over the figures and apply each command like
for i in (1,2):
plt.figure(i)
plt.xlim(0,3)
Now those are three lines of code. If the requirement is to use a single line of code, the following would be a solution
[plt.setp(plt.figure(i).axes[0], xlim=(0,3)) for i in plt.get_fignums() if i in (1,2)]
This is neither elegant nor easy to type, so when using pyplot I would recommend the first solution.
In general however I would recommend using the object oriented approach, where creating two figures would look like:
import matplotlib.pyplot as plt
a=[1,2,3]
b=[3,4,5]
fig, ax = plt.subplots()
ax.plot(b)
fig2, ax2 = plt.subplots()
ax2.plot(a)
plt.show()
Then the single line solution is also a bit more compact
plt.setp([ax,ax2], xlim=(0,3))

Python plt: close or clear figure does not work

I generate a lots of figures with a script which I do not display but store to harddrive. After a while I get the message
/usr/lib/pymodules/python2.7/matplotlib/pyplot.py:412: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (matplotlib.pyplot.figure) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam figure.max_num_figures).
max_open_warning, RuntimeWarning)
Thus, I tried to close or clear the figures after storing. So far, I tried all of the followings but no one works. I still get the message from above.
plt.figure().clf()
plt.figure().clear()
plt.clf()
plt.close()
plt.close('all')
plt.close(plt.figure())
And furthermore I tried to restrict the number of open figures by
plt.rcParams.update({'figure.max_num_figures':1})
Here follows a piece of sample code that behaves like described above. I added the different options I tried as comments at the places I tried them.
from pandas import DataFrame
from numpy import random
df = DataFrame(random.randint(0,10,40))
import matplotlib.pyplot as plt
plt.ioff()
#plt.rcParams.update({'figure.max_num_figures':1})
for i in range(0,30):
fig, ax = plt.subplots()
ax.hist([df])
plt.savefig("/home/userXYZ/Development/pic_test.png")
#plt.figure().clf()
#plt.figure().clear()
#plt.clf()
#plt.close() # results in an error
#plt.close('all') # also error
#plt.close(plt.figure()) # also error
To be complete, that is the error I get when using plt.close:
can't invoke "event" command: application has been destroyed
while executing "event generate $w <>"
(procedure "ttk::ThemeChanged" line 6)
invoked from within "ttk::ThemeChanged"
The correct way to close your figures would be to use plt.close(fig), as can be seen in the below edit of the code you originally posted.
from pandas import DataFrame
from numpy import random
df = DataFrame(random.randint(0,10,40))
import matplotlib.pyplot as plt
plt.ioff()
for i in range(0,30):
fig, ax = plt.subplots()
ax.hist(df)
name = 'fig'+str(i)+'.png' # Note that the name should change dynamically
plt.savefig(name)
plt.close(fig) # <-- use this line
The error that you describe at the end of your question suggests to me that your problem is not with matplotlib, but rather with another part of your code (such as ttk).
plt.show() is a blocking function, so in the above code, plt.close() will not execute until the fig windows are closed.
You can use plt.ion() at the beginning of your code to make it non-blocking. Even though this has some other implications the fig will be closed.
I was still having the same issue on Python 3.9.7, matplotlib 3.5.1, and VS Code (the issue that no combination of plt.close() closes the figure). I have three loops which the most inner loop plots more than 20 figures. The solution that is working for me is using agg as backend and del someFig after plt.close(someFig). Subsequently, the order of code would be something like:
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
someFig = plt.figure()
.
.
.
someFig.savefig('OUTPUT_PATH')
plt.close(someFig) # --> (Note 1)
del someFig
.
.
.
NOTE 1: If this line is removed, the output figures may not be plotted correctly! Especially when the number of elements to be rendered in the figure is high.
NOTE 2: I don't know whether this solution could backfire or not, but at least it is working and not hugging RAM or preventing plotting figures!
import tensorflow as tf
from matplotlib import pyplot as plt
sample_image = tf.io.read_file(str(PATH / 'Path to your file'))
sample_image = tf.io.decode_jpeg(sample_image)
print(sample_image.shape)
plt.figure("1 - Sample Image ")
plt.title(label="Sample Image", fontsize=12, color="red")
plt.imshow(sample_image)
plt.show(block=False)
plt.pause(3)
plt.close()
plt.show(block=False)
plt.pause(interval) do the trick
This does not really solve my problem, but it is a work-around to handle the high memory consumption I faced and I do not get any of the error messages as before:
from pandas import DataFrame
from numpy import random
df = DataFrame(random.randint(0,10,40))
import matplotlib.pyplot as plt
plt.ioff()
for i in range(0,30):
plt.close('all')
fig, ax = plt.subplots()
ax.hist([df])
plt.savefig("/home/userXYZ/Development/pic_test.png")

Matplotlib can't suppress figure window

I'm having trouble with matplotlib insisting on displaying a figure wnidow even when I haven't called show().
The function in question is:
def make_plot(df):
fig, axes = plt.subplots(3, 1, figsize=(10, 6), sharex=True)
plt.subplots_adjust(hspace=0.2)
axes[0].plot(df["Date_Time"], df["T1"], df["Date_Time"], df["T2"])
axes[0].set_ylabel("Temperature (C)")
axes[0].legend(["T1", "T2"], bbox_to_anchor=(1.12, 1.1))
axes[1].semilogy(df["Date_Time"], df["IGP"], df["Date_Time"], df["IPP"])
axes[1].legend(["IGP", "IPP"], bbox_to_anchor=(1.12, 1.1))
axes[1].set_ylabel("Pressure (mBar)")
axes[2].plot(df["Date_Time"], df["Voltage"], "k")
axes[2].set_ylabel("Voltage (V)")
current_axes = axes[2].twinx()
current_axes.plot(df["Date_Time"], df["Current"], "r")
current_axes.set_ylabel("Current (mA)")
axes[2].legend(["V"], bbox_to_anchor=(1.15, 1.1))
current_axes.legend(["I"], bbox_to_anchor=(1.14, 0.9))
plt.savefig("static/data.png")
where df is a dataframe created using pandas. This is supposed to be in the background of a web server, so all I want is for this function to drop the file in the directory specified. However, when it executes it does this, and then pulls up a figure window and gets stuck in a loop, preventing me from reloading the page. Am I missing something obvious?
EDIT: Forgot to add, I am running python 2.7 on Windows 7, 64 bit.
Step 1
Check whether you're running in interactive mode. The default is non-interactive, but you may never know:
>>> import matplotlib as mpl
>>> mpl.is_interactive()
False
You can set the mode explicitly to non-interactive by using
>>> from matplotlib import pyplot as plt
>>> plt.ioff()
Since the default is non-interactive, this is probably not the problem.
Step 2
Make sure your backend is a non-gui backend. It's the difference between using Agg versus TkAgg, WXAgg, GTKAgg etc, the latter being gui backends, while Agg is a non-gui backend.
You can set the backend in a number of ways:
in your matplotlib configuration file; find the line starting with backend:
backend: Agg
at the top of your program with the global matplotlib function use:
matplotlib.use('Agg')
import the canvas directly from the correct backend; this is most useful in non-pyplot "mode" (OO-style), which is what I often use, and for a webserver style of use, that may in the end prove best (since this is a tad different than above, here's a full-blown short example):
import numpy as np
from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
figure = Figure()
canvas = FigureCanvas(figure)
axes = figure.add_subplot(1, 1, 1)
axes.plot(x, np.sin(x), 'k-')
canvas.print_figure('sine.png')
Perhaps just clear the axis, for example:
plt.savefig("static/data.png")
plt.close()
will not plot the output in inline mode. I can't work out if is really clearing the data though.
use below:
plt.rcParams['figure.subplot.hspace'] = 0.002
## The figure subplot parameters. All dimensions are a fraction of the figure width and height.
#figure.subplot.left: 0.125 # the left side of the subplots of the figure
#figure.subplot.right: 0.9 # the right side of the subplots of the figure
#figure.subplot.bottom: 0.11 # the bottom of the subplots of the figure
#figure.subplot.top: 0.88 # the top of the subplots of the figure
#figure.subplot.wspace: 0.2 # the amount of width reserved for space between subplots,
# expressed as a fraction of the average axis width
#figure.subplot.hspace: 0.2 # the amount of height reserved for space between subplots,
# expressed as a fraction of the average axis height
instead of
plt.subplots_adjust(hspace=0.2)
reference urls:
Customizing Matplotlib with style sheets and rcParams
matplotlib.pyplot.subplots_adjust

How do I set the aspect ratio for a plot in Python with Spyder?

I'm brand new to Python, I just switched from Matlab. The distro is Anaconda 2.1.0 and I'm using the Spyder IDE that came with it.
I'm trying to make a scatter plot with equal ratios on the x and y axes, so that this code prints a square figure with the vertices of a regular hexagon plotted inside.
import numpy
import cmath
import matplotlib
coeff = [1,0,0,0,0,0,-1]
x = numpy.roots(coeff)
zeroplot = plot(real(x),imag(x), 'ro')
plt.gca(aspect='equal')
plt.show()
But plt.gca(aspect='equal') returns a blank figure with axes [0,1,0,1], and plt.show() returns nothing.
I think the main problem is that plt.gca(aspect='equal') doesn't just grab the current axis and set its aspect ratio. From the documentation, (help(plt.gca)) it appears to create a new axis if the current one doesn't have the correct aspect ratio, so the immediate fix for this should be to replace plt.gca(aspect='equal') with:
ax = plt.gca()
ax.set_aspect('equal')
I should also mention that I had a little bit of trouble getting your code running because you're using pylab to automatically load numpy and matplotlib functions: I had to change my version to:
import numpy
import cmath
from matplotlib import pyplot as plt
coeff = [1,0,0,0,0,0,-1]
x = numpy.roots(coeff)
zeroplot = plt.plot(numpy.real(x), numpy.imag(x), 'ro')
ax = plt.gca()
ax.set_aspect('equal')
plt.show()
People who are already comfortable with Python don't generally use Pylab, from my experience. In future you might find it hard to get help on things if people don't realise that you're using Pylab or aren't familiar with how it works. I'd recommend disabling it and trying to get used to accessing the functions you need through their respective modules (e.g. using numpy.real instead of just real)

Combining mayavi and matplotlib in the same figure

I will be making animations. In each frame I want to contain both a mayavi plot obtained with
mlab.pipeline.iso_surface(source, some other superfluous args)
and a matplotlib plot obtained using simply
pylab.plot(args)
I have scripts to do both separately, but have no idea how to go about combining them into one figure. I want the end product to be one script which contains the code from both the scripts that I currently have.
AFAIK, there is no direct way because the backends used are so different. It does not seem possible to add matplotlib axes to mayavi.figure or vice versa.
However, there is a "kind of a way" by using the the mlab.screenshot.
import mayavi.mlab as mlab
import matplotlib.pyplot as plt
# create and capture a mlab object
mlab.test_plot3d()
img = mlab.screenshot()
mlab.close()
# create a pyplot
fig = plt.figure()
ax1 = fig.add_subplot(121)
ax1.plot([0,1], [1,0], 'r')
# add the screen capture
ax2 = fig.add_subplot(122)
ax2.imshow(img)
ax2.set_axis_off()
This is not necessarily the nicest possible way of doing things, and you may bump into resolution problems, as well (check the size of the mayavi window). However, it gets the job done in most cases.
Adding to the answer by DrV which helped me a great deal, you can work with the mlab figure to set resolution before screenshot such as with batch plotting:
mfig = mlab.figure(size=(1024, 1024))
src = mlab.pipeline.scalar_field(field_3d_numpy_array)
mlab.pipeline.iso_surface(src)
iso_surface_plot = mlab.screenshot(figure=mfig, mode='rgba', antialiased=True)
mlab.clf(mfig)
mlab.close()
# Then later in a matplotlib fig:
plt.imshow(iso_surface_plot)

Categories

Resources