matplotlib auto yaxis - python

I'm using matplotlib to interactively view a large dataset. I'd like to zoom/pan the view without redrawing it for performance reasons. It's easy to create the new x view range and apply it with set_xlim(left,right). But there does not seem to be an automatic way to show the new view with the y axis appropriately value limited based on the new x range.
autoscale(enable=True, axis='y') finds the y max and min from all the data drawn, and not restricted to the y data based on the x view limit. Is there any such mode or function, or must set_ylim() be used with a manually calculated range?
Here is an example:
#! /usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np
plt.ioff()
fig = plt.figure(1)
ax = plt.subplot(111, axisbg='black')
tick = np.arange(10)
ax.grid(b=True, color='white')
ax.autoscale(enable=True, axis='x', tight=False)
ax.autoscale(enable=True, axis='y', tight=True)
ax.plot(tick, color='#ff3333', linestyle='-')
fig.canvas.draw()
raw_input('enter:')
ax.set_xlim(2, 5)
fig.canvas.draw()
raw_input('enter:')
ax.set_xlim(1, 6)
fig.canvas.draw()
raw_input('enter:')

I'm sorry, I submitted an answer to the wrong question!
But I should think that it is not that hard to manually calculate the range. Or you can use plt.ion() and zoom in appropriately with the magnifying glass.

I never did find a way through matplotlib to determine the rendered y limits based on x.
However, two factors seem to obviate the need for this.
matplotlib 1.2.1 has big performance gains over the previous version I was using.
For OS X, changing the backend from 'MacOSX' to 'TkAgg' saw big performance improvements for the line styles being used.
In sum, the gains were on the order of 10-20x, which made the interactive browsing of the dataset real-time, so no additional hacks were needed.

Related

Cancelling displaying a Matplotlib plot [duplicate]

Matplotlib offers these functions:
cla() # Clear axis
clf() # Clear figure
close() # Close a figure window
When should I use each function and what exactly does it do?
They all do different things, since matplotlib uses a hierarchical order in which a figure window contains a figure which may consist of many axes. Additionally, there are functions from the pyplot interface and there are methods on the Figure class. I will discuss both cases below.
pyplot interface
pyplot is a module that collects a couple of functions that allow matplotlib to be used in a functional manner. I here assume that pyplot has been imported as import matplotlib.pyplot as plt.
In this case, there are three different commands that remove stuff:
See matplotlib.pyplot Functions:
plt.cla() clears an axis, i.e. the currently active axis in the current figure. It leaves the other axes untouched.
plt.clf() clears the entire current figure with all its axes, but leaves the window opened, such that it may be reused for other plots.
plt.close() closes a window, which will be the current window, if not specified otherwise.
Which functions suits you best depends thus on your use-case.
The close() function furthermore allows one to specify which window should be closed. The argument can either be a number or name given to a window when it was created using figure(number_or_name) or it can be a figure instance fig obtained, i.e., usingfig = figure(). If no argument is given to close(), the currently active window will be closed. Furthermore, there is the syntax close('all'), which closes all figures.
methods of the Figure class
Additionally, the Figure class provides methods for clearing figures.
I'll assume in the following that fig is an instance of a Figure:
fig.clf() clears the entire figure. This call is equivalent to plt.clf() only if fig is the current figure.
fig.clear() is a synonym for fig.clf()
Note that even del fig will not close the associated figure window. As far as I know the only way to close a figure window is using plt.close(fig) as described above.
There is just a caveat that I discovered today.
If you have a function that is calling a plot a lot of times you better use plt.close(fig) instead of fig.clf() somehow the first does not accumulate in memory. In short if memory is a concern use plt.close(fig) (Although it seems that there are better ways, go to the end of this comment for relevant links).
So the the following script will produce an empty list:
for i in range(5):
fig = plot_figure()
plt.close(fig)
# This returns a list with all figure numbers available
print(plt.get_fignums())
Whereas this one will produce a list with five figures on it.
for i in range(5):
fig = plot_figure()
fig.clf()
# This returns a list with all figure numbers available
print(plt.get_fignums())
From the documentation above is not clear to me what is the difference between closing a figure and closing a window. Maybe that will clarify.
If you want to try a complete script there you have:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(1000)
y = np.sin(x)
for i in range(5):
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(x, y)
plt.close(fig)
print(plt.get_fignums())
for i in range(5):
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(x, y)
fig.clf()
print(plt.get_fignums())
If memory is a concern somebody already posted a work-around in SO see:
Create a figure that is reference counted
plt.cla() means clear current axis
plt.clf() means clear current figure
also, there's plt.gca() (get current axis) and plt.gcf() (get current figure)
Read more here: Matplotlib, Pyplot, Pylab etc: What's the difference between these and when to use each?

pandas DataFrame.plot "no numeric data to plot" while plotting the second time [duplicate]

The following example code generates a simple plot, then saves it to 'fig1.pdf', then displays it, then saves it again to 'fig2.pdf'. The first image looks as expected, but the second one is blank (contains a white square). What's actually going on here? The line plt.show() apparently messes something up, but I can't figure out what/how!
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1, 1, 100)
y = x**2
plt.plot(x,y)
plt.savefig('fig1.pdf')
plt.show()
plt.savefig('fig2.pdf')
If you want to save the figure after displaying it, you'll need to hold on to the figure instance. The reason that plt.savefig doesn't work after calling show is that the current figure has been reset.
pyplot keeps track of which figures, axes, etc are "current" (i.e. have not yet been displayed with show) behind-the-scenes. gcf and gca get the current figure and current axes instances, respectively. plt.savefig (and essentially any other pyplot method) just does plt.gcf().savefig(...). In other words, get the current figure instance and call its savefig method. Similarly plt.plot basically does plt.gca().plot(...).
After show is called, the list of "current" figures and axes is empty.
In general, you're better off directly using the figure and axes instances to plot/save/show/etc, rather than using plt.plot, etc, to implicitly get the current figure/axes and plot on it. There's nothing wrong with using pyplot for everything (especially interactively), but it makes it easier to shoot yourself in the foot.
Use pyplot for plt.show() and to generate a figure and an axes object(s), but then use the figure or axes methods directly. (e.g. ax.plot(x, y) instead of plt.plot(x, y), etc) The main advantage of this is that it's explicit. You know what objects you're plotting on, and don't have to reason about what the pyplot state-machine does (though it's not that hard to understand the state-machine interface, either).
As an example of the "recommended" way of doing things, do something like:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1, 1, 100)
y = x**2
fig, ax = plt.subplots()
ax.plot(x, y)
fig.savefig('fig1.pdf')
plt.show()
fig.savefig('fig2.pdf')
If you'd rather use the pyplot interface for everything, then just grab the figure instance before you call show. For example:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1, 1, 100)
y = x**2
plt.plot(x, y)
fig = plt.gcf()
fig.savefig('fig1.pdf')
plt.show()
fig.savefig('fig2.pdf')
pyplot.show blocks and destroys the plot upon closing. You can use
plt.show(block=False)
after which the save to fig2.pdf will work or you can plot it again before saving
plt.plot(x,y)
plt.savefig('fig2.pdf')
I had to run plt.cla() and plt.clf() before plotting the second one. Clear current axes and clear current plot, respectively.
If you just want to see the figure before saving, you can call
plt.ion()
before plotting, which starts interactive mode, and shows all figures as they are drawn. This mostly removes the need to call plt.show(). You no longer need to close the figures to continue.
To disable interactive mode again, call plt.ioff().

Why does the python plotting example in the body below use subplot when it appears to add no value?

The matplotlib documentation
https://matplotlib.org/gallery/lines_bars_and_markers/simple_plot.html
provides the following example code
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
fig, ax = plt.subplots()
ax.plot(t, s)
ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='About as simple as it gets, folks')
ax.grid()
fig.savefig("test.png")
plt.show()
to produce the following plot
The exact same plot can be produced with the following code
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
plt.plot(t,s)
plt.grid(True)
plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.title('About as simple as it gets, folks')
plt.savefig("test.png")
plt.show()
which makes no use of subplot, ax, or fig.
I learned how to plot using the second approach. I've stumbled upon the first approach only recently
and although I can kind of imagine what it's doing, it does seem far less intuitive than the second approach.
Can somebody explain the rational behind the first approach as it seems to complicate something very simple?
The pyplot approach may look more intuitive at first sight indeed. And of course that is to a large extent subjective. So feel free to use it if it suits your needs.
pyplot is a so-called state machine. It will operate on the "current figure" and "current axes". However, it will then, like in the first case, call the respective methods of the current objects. E.g. plt.plot(...) will just call ax = plt.gca(); ax.plot(...) internally anyways. So one might decide to directly work with the objects in question, instead of relying on pyplot to find those.
Also, pyplot does not expose matplotlib's complete functionality. E.g. if you want to create a formatter for your ticklabels, you will need to use the object oriented interface anyways,
plt.gca().xaxis.set_major_formatter(...)
in which case one might also be tempted to directly have an axes ax at one's disposal to work on
ax.xaxis.set_major_formatter(...)
Equally if you want to write a function to plot something to an axes, you might let this function take the axes object as input
def myplot(ax, data, **kwargs):
ax.plot(data, label = "My plot", **kwargs)
ax.set_title("My plot")
ax.legend()
Then reusing such function on any axes is pretty handy. Maybe you have a single figure with an axes to plot to,
fig, ax = plt.subplots()
myplot(ax, data)
or you have several axes in different figures
fig1, (ax1, ax2) = plt.subplots(ncols=2)
fig2, (ax3, ax4) = plt.subplots(ncols=2)
myplot(ax2, data2)
myplot(ax4, data3)
There are also cases, where you don't want to use pyplot at all, e.g. when embedding your plot into a GUI, in which case you'd need to use a structure like
fig = matplotlib.figure.Figure()
canvas = FigureCanvasQT(fig)
axes = fig.subplots(2,3)
axes[0,1].plot(...)
Finally, it should be noted that a lot of issues that people ask about here on Stackoverflow directly result from pyplot being used instead of the object-oriented approach. That's not to say, you shouldn't use it, but in overall terms it seems like pyplot increases the chances for shooting yourself in the foot if you don't pay maximum attention to what the "current axes" is while working with pyplot.
Due to all of the above, matplotlib came to the conclusion that it's best to advocate using the object-oriented approach, even though it might, for very simple examples like the ones from the question, be less straight forward to use. Even if not using the object oriented style, understanding it is somehow a prerequisite of using matplotlib even for midly complex plots.

Saving a plot into a png file in Python [duplicate]

The following example code generates a simple plot, then saves it to 'fig1.pdf', then displays it, then saves it again to 'fig2.pdf'. The first image looks as expected, but the second one is blank (contains a white square). What's actually going on here? The line plt.show() apparently messes something up, but I can't figure out what/how!
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1, 1, 100)
y = x**2
plt.plot(x,y)
plt.savefig('fig1.pdf')
plt.show()
plt.savefig('fig2.pdf')
If you want to save the figure after displaying it, you'll need to hold on to the figure instance. The reason that plt.savefig doesn't work after calling show is that the current figure has been reset.
pyplot keeps track of which figures, axes, etc are "current" (i.e. have not yet been displayed with show) behind-the-scenes. gcf and gca get the current figure and current axes instances, respectively. plt.savefig (and essentially any other pyplot method) just does plt.gcf().savefig(...). In other words, get the current figure instance and call its savefig method. Similarly plt.plot basically does plt.gca().plot(...).
After show is called, the list of "current" figures and axes is empty.
In general, you're better off directly using the figure and axes instances to plot/save/show/etc, rather than using plt.plot, etc, to implicitly get the current figure/axes and plot on it. There's nothing wrong with using pyplot for everything (especially interactively), but it makes it easier to shoot yourself in the foot.
Use pyplot for plt.show() and to generate a figure and an axes object(s), but then use the figure or axes methods directly. (e.g. ax.plot(x, y) instead of plt.plot(x, y), etc) The main advantage of this is that it's explicit. You know what objects you're plotting on, and don't have to reason about what the pyplot state-machine does (though it's not that hard to understand the state-machine interface, either).
As an example of the "recommended" way of doing things, do something like:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1, 1, 100)
y = x**2
fig, ax = plt.subplots()
ax.plot(x, y)
fig.savefig('fig1.pdf')
plt.show()
fig.savefig('fig2.pdf')
If you'd rather use the pyplot interface for everything, then just grab the figure instance before you call show. For example:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1, 1, 100)
y = x**2
plt.plot(x, y)
fig = plt.gcf()
fig.savefig('fig1.pdf')
plt.show()
fig.savefig('fig2.pdf')
pyplot.show blocks and destroys the plot upon closing. You can use
plt.show(block=False)
after which the save to fig2.pdf will work or you can plot it again before saving
plt.plot(x,y)
plt.savefig('fig2.pdf')
I had to run plt.cla() and plt.clf() before plotting the second one. Clear current axes and clear current plot, respectively.
If you just want to see the figure before saving, you can call
plt.ion()
before plotting, which starts interactive mode, and shows all figures as they are drawn. This mostly removes the need to call plt.show(). You no longer need to close the figures to continue.
To disable interactive mode again, call plt.ioff().

Saving a figure after invoking pyplot.show() results in an empty file

The following example code generates a simple plot, then saves it to 'fig1.pdf', then displays it, then saves it again to 'fig2.pdf'. The first image looks as expected, but the second one is blank (contains a white square). What's actually going on here? The line plt.show() apparently messes something up, but I can't figure out what/how!
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1, 1, 100)
y = x**2
plt.plot(x,y)
plt.savefig('fig1.pdf')
plt.show()
plt.savefig('fig2.pdf')
If you want to save the figure after displaying it, you'll need to hold on to the figure instance. The reason that plt.savefig doesn't work after calling show is that the current figure has been reset.
pyplot keeps track of which figures, axes, etc are "current" (i.e. have not yet been displayed with show) behind-the-scenes. gcf and gca get the current figure and current axes instances, respectively. plt.savefig (and essentially any other pyplot method) just does plt.gcf().savefig(...). In other words, get the current figure instance and call its savefig method. Similarly plt.plot basically does plt.gca().plot(...).
After show is called, the list of "current" figures and axes is empty.
In general, you're better off directly using the figure and axes instances to plot/save/show/etc, rather than using plt.plot, etc, to implicitly get the current figure/axes and plot on it. There's nothing wrong with using pyplot for everything (especially interactively), but it makes it easier to shoot yourself in the foot.
Use pyplot for plt.show() and to generate a figure and an axes object(s), but then use the figure or axes methods directly. (e.g. ax.plot(x, y) instead of plt.plot(x, y), etc) The main advantage of this is that it's explicit. You know what objects you're plotting on, and don't have to reason about what the pyplot state-machine does (though it's not that hard to understand the state-machine interface, either).
As an example of the "recommended" way of doing things, do something like:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1, 1, 100)
y = x**2
fig, ax = plt.subplots()
ax.plot(x, y)
fig.savefig('fig1.pdf')
plt.show()
fig.savefig('fig2.pdf')
If you'd rather use the pyplot interface for everything, then just grab the figure instance before you call show. For example:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1, 1, 100)
y = x**2
plt.plot(x, y)
fig = plt.gcf()
fig.savefig('fig1.pdf')
plt.show()
fig.savefig('fig2.pdf')
pyplot.show blocks and destroys the plot upon closing. You can use
plt.show(block=False)
after which the save to fig2.pdf will work or you can plot it again before saving
plt.plot(x,y)
plt.savefig('fig2.pdf')
I had to run plt.cla() and plt.clf() before plotting the second one. Clear current axes and clear current plot, respectively.
If you just want to see the figure before saving, you can call
plt.ion()
before plotting, which starts interactive mode, and shows all figures as they are drawn. This mostly removes the need to call plt.show(). You no longer need to close the figures to continue.
To disable interactive mode again, call plt.ioff().

Categories