I would like to plot two or more graphs at once using python and matplotlib. I do not want to use subplot since it is actually two or more plots on the same drawing paper.
Is there any way to do it?
You can use multiple figures and plot some data in each of them. The easiest way of doing so is to call plt.figure() and use the pyplot statemachine.
import matplotlib.pyplot as plt
plt.figure() # creates a figure
plt.plot([1,2,3])
plt.figure() # creates a new figure
plt.plot([3,2,1])
plt.show() # opens a window for each of the figures
If for whatever reason after creating a second figure you want to plot to the first one, you need to 'activate' it via
plt.figure(1)
plt.plot([2,3,1]) # this is plotted to the first figure.
(Figure numbers start at 1)
Related
import pandas as pd
import seaborn as sns
# load data
df = sns.load_dataset('penguins', cache=False)
sns.scatterplot(data=df, x='bill_length_mm', y='bill_depth_mm', hue='sex')
plt.show()
sns.scatterplot(data=df, x='flipper_length_mm', y='body_mass_g', hue='sex')
plt.show()
When I draw two plots with seaborn, in one cell, in jupyter, I get this view:
I want to draw the plots, side by side, like this:
plot1 plot2
How I should do this?
Updated:
Not two plots on one figure, but two plots on two separate figures.
This is not the solution being sought, because it's two plots on one figure.
fig, ax = plt.subplots(1,2)
sns.plotType(someData, ax=ax[0]) # plot1
sns.plotType(someData, ax=ax[1]) # plot2
fig.show()
The solutions from the proposed duplicate ipython notebook arrange plots horizontally, do not work
The option with %html causes the figures to plot on top of each other
Additionally, other options were for ipython, not Jupyter, or recommended creating subplots..
This is probably the simplest solution. Other solutions would likely involve hacking the backend environment of Jupyter.
This question is about displaying two figures, side by side.
Two separate figures, side by side, executed from a code cell, does not work.
You will need to create the separate figures, and the use plt.savefig('file.jpg') to save each figure to a file.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# load data
df = sns.load_dataset('penguins', cache=False)
# create and save figure
sns.scatterplot(data=df, x='bill_length_mm', y='bill_depth_mm', hue='sex')
plt.savefig('bill.jpg')
plt.close() # prevents figure from being displayed when code cell is executed
# create and save new figure
sns.scatterplot(data=df, x='flipper_length_mm', y='body_mass_g', hue='sex')
plt.savefig('flipper.jpg')
plt.close() # prevents figure from being displayed when code cell is executed
Once the figures are saved to a file, they can be displayed side by side, by loading them in a markdown cell.
If the images are to large, the second figure will go to a new line.
Then execute the cell
I am familiar with the idea of putting several plots in an overlay by calling the plot function several times
plt.plot(f1)
plt.plot(f2)
plt.plot(f2)
plt.show()
But what if I want to plot data that I have already put on a canvas?
i was thinking to use the matplotlib.lines.Line2D object returned by each plt.plot. If I store them in suitable variables then I can reuse them at a later time. But how do I get a plot out of these matplotlib.lines.Line2D? Maybe I am trying a doomed strategy
p3000=plt.plot([1,2,3])
and then
fig, ax = plt.subplots()
ax.add_line(p3000[0])
but I get RuntimeError: Can not put single artist in more than one figure
I have also tried get_xydata member but ti does not give the points in a form I am able to read.
I am trying to embed matplotlib figure into PyQt4, but when I try to plot the figure using pyplot, it's giving an error or would make a separate matplotlib built-in figure object rather than plotting into my canvas.
If I try to plot using axes, all is good as figure gets plotted into the canvas. However, I can't plot two axes into a single graph which was done by pyplot by simply plotting two plots one after another.
So how can I 2 plots into one graph using axes?
class Foo(blah):
""" some code here """
# code which giving error:
self.pyplot.plot_date(x,y)
self.pyplot.plot_date(a,b)
# code plotting matplotlib figure object rather drawing into canvas
pyplot.plot_date(x,y)
pyplot.plot_date(a,b)
# code only plotting only a,b - but not x,y
self.axes.plot_date(x,y)
self.axes.plot_date(a,b)
I'm making something using Matplotlib where I have multiple subplots on a figure. It seems to me like the subplot API is limited compared to the PyPlot API: for example, I can't seem to make custom axes labels in my subplot although it is possible using PyPlot.
My question is: Is there a richer subplot API besides the tiny one on the PyPlot page (http://matplotlib.org/api/pyplot_api.html), and/or is there a way to get the full functionality of a PyPlot on a subplot?
Basically, what is a subplot? I can't find it in the documentation. Even more generally, when should I use a figure vs an axis vs a subplot? They all seem to do essentially the same thing.
Consider the following code:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(2,1,1)
Then ax is an axis? Can I use the pyplot API to customize ax?
Thanks for your help.
While i suggest that use the axes methods, there is the plt.sca function (set current axes).
So
plt.sca(ax)
does what you want, i think.
I am new to using PyLab. I want to plot some points. But I don't want to show the previous points i.e. as a new point comes the previous plotted point will vanish and the new point will be plotted. I have searched a lot but I could not find how to re-initialize the plot in between. The problem I am facing is I can set the current figure by using
plt.figure(f1.number) but after plotting the point in that figure it gets permanently changed.
plt.hold(False) before you start plotting will do what you want.
hold determines of old artists are held-on to when new ones are plotted. The default is for hold to be on.
ex
# two lines
plt.figure()
plt.hold(True)
plt.plot(range(5))
plt.plot(range(5)[::-1])
#one line
plt.figure()
plt.hold(False)
plt.plot(range(5))
plt.plot(range(5)[::-1])
Changing it via plt.hold changes it for all (new) axes. You can change the hold state for an individual axes by
ax = gca()
ax.hold(True)
With pylab, pylab.clf() should clear the figure, after which you can redraw the plot.
Alternatively, you can update your data with set_xdata and set_ydata that are methods on the axes object that gets returned when you create a new plot (either with pylab.plot or pylab.subplot).
The latter is probably preferred, but requires a litte more work. One example I can quickly find is another SO question.