Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib Axes.plot() vs pyplot.plot()

What is the difference between the Axes.plot() and pyplot.plot() methods? Does one use another as a subroutine?

It seems that my options for plotting are

line = plt.plot(data) 

or

ax = plt.axes() line = ax.plot(data) 

or even

fig = plt.figure() ax = fig.add_axes([0,0,1,1]) line = ax.plot(data) 

Are there situations where it is preferable to use one over the other?

like image 382
dkv Avatar asked Apr 18 '17 21:04

dkv


People also ask

What is difference between Pyplot and matplotlib?

The matplotlib. pyplot is a collection of functions that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.

What is the difference between PLT plot and ax plot?

MatPlotLib with PythonPlot − Plot helps to plot just one diagram with (x, y) coordinates. Axes − Axes help to plot one or more diagrams in the same window and sets the location of the figure.

What is the difference between axes and axis in matplotlib?

Axis is the axis of the plot, the thing that gets ticks and tick labels. The axes is the area your plot appears in.

What is the use of matplotlib Pyplot plot () function?

Plotting x and y points The plot() function is used to draw points (markers) in a diagram. By default, the plot() function draws a line from point to point. The function takes parameters for specifying points in the diagram. Parameter 1 is an array containing the points on the x-axis.


Video Answer


2 Answers

For drawing a single plot, the best practice is probably

fig = plt.figure() plt.plot(data) fig.show() 

Now, lets take a look in to 3 examples from the question and explain what they do.

  1. Takes the current figure and axes (if none exists it will create a new one) and plot into them.

    line = plt.plot(data) 
  2. In your case, the behavior is same as before with explicitly stating the axes for plot.

     ax = plt.axes()  line = ax.plot(data) 

This approach of using ax.plot(...) is a must, if you want to plot into multiple axes (possibly in one figure). For example when using a subplots.

  1. Explicitly creates new figure - you will not add anything to previous one. Explicitly creates a new axes with given rectangle shape and the rest is the same as with 2.

     fig = plt.figure()  ax = fig.add_axes([0,0,1,1])  line = ax.plot(data) 

possible problem using figure.add_axes is that it may add a new axes object to the figure, which will overlay the first one (or others). This happens if the requested size does not match the existing ones.

like image 119
matusko Avatar answered Oct 01 '22 08:10

matusko


There is essentially no difference. plt.plot will at some point (after making sure that there is a figure and an axes available to plot to) call the plot function from that axes instance.

So the main difference is rather at the user's side:

  • do you want to use the Matlab-like state machine approach, which may save some lines of code for simple plotting tasks? Then use pyplot.
  • do you want to have full control over the plotting using the more pythonic object oriented approach? Then use objects like axes explicitely.

You may want to read the matplotlib usage guide.

like image 27
ImportanceOfBeingErnest Avatar answered Oct 01 '22 10:10

ImportanceOfBeingErnest