What is the difference between axes and pyplot - python

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)

Related

Creating a plotly vrect over all axis of a matplotlib plot

I have a complete matplotlib figure with multiple subplots and I want to add a vertical span in a specific range to all of the subplots. The remaining plot is then converted to plotly.
If I try running xvspan over each individual subplot when creating the matplotlib figure, the vertical spans do not make it into the plotly figure.
If I try converting the figure into plotly first and then adding a vrect, this vrect only seems to work for the first subplot. I have tried using row = "all" to no avail.
Any other ideas?
Thanks!

create multiple cells in a loop in notebook [duplicate]

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)

Overlay two plots *after* I have made plt.show() in Matplotlib

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.

matplotlib figure vs subplot vs axes?

Though I've been able to create basic plots with matplotlib.pyplot before, I cannot fully understand what the difference between these are:
figure
subplot
axes
(axis?)
As I understand, all three of these are objects for the area in which a graph is drawn, so what distinguishes them in terms of what can be drawn inside them?
Matplotlib's user guide gives a great description about the parts of a figure.
The components are hierarchical:
Figure object: the whole figure
Axes object: belongs to a Figure object and is the space where we add the data to visualize
Axis object: belongs to an Axes object. Axis object can be categorized as XAxis or YAxis.
For further info, visit this link:
Getting Started with Matplotlib

show two plot together in Matplotlib like show(fig1,fig2)

In general, I don't have any problem to put two plots in a figure like plot(a);plot(b) in matplotlib. Now I am using a particular library which would generate a figure and I want to overlay with boxplot. Both are generated by matplotlib. So I think it should be fine but I can only see one plot. Here is the code. I am using beeswarm and here is its ipython notebook. I can only plot beeswarm or boxplot but not both in a figure. My main goal is trying to save column scatter plot and boxplot together as a figure in pdf. Thanks,
from beeswarm import beeswarm
fig=plt.figure()
figure(figsize=(5,7))
ax1=plt.subplot(111)
fig.ylim=(0,11)
d2 = np.random.random_integers(10,size=100)
beeswarm(d2,col="red",method="swarm",ax=ax1,ylim=(0,11))
boxplot(d2)
The problem is with the positioning of the box plot. The default positioning list starts with 1 what shifts the plot to 1 and your beeswarm plot sits on 0.
So the plots are in different places of your canvas.
I modified your code a little bit and that seems to solve your problem.
from beeswarm import beeswarm
fig = plt.figure(figsize=(5,7))
ax1 = fig.add_subplot(111)
# Here you may want to use ax1.set_ylim(0,11) instead fig.ylim=(0,11)
ax1.set_ylim(0,11)
d2 = np.random.random_integers(10,size=100)
beeswarm(d2,col="red",method="swarm",ax=ax1,ylim=(0,11))
boxplot(d2,positions=[0])
Cheers

Categories

Resources