Reputation: 119
After a lot of fighting and watching youtube tutorials I have been able to achieve my goal of creating a figure window, with a unique name and 4 subplots.
The method I used was add_subplot
because I understand the code better rather than unpacking tuples method i.e. fig, ...
The last bit I am trying to do is share my x axis and after a lot of playing around i.e. trying to insert the code sharex="Cols"
or sharex = True
I have had no success.
My complete code is as follows:-
import pandas
import numpy
from matplotlib import pyplot
x = numpy.linspace(0, 2*numpy.pi, 400)
y = numpy.sin(x**2)
yy = numpy.sin(x**2+3)
My_Figure_Window = pyplot.figure("Title Of Figure Window")
pyplot.style.use("ggplot")
My_Figure_Window.suptitle("Main Title")
Sub_Plot_Rows = 4
Sub_Plot_Cols = 1
Wall = My_Figure_Window.add_subplot(Sub_Plot_Rows, Sub_Plot_Cols, 1)
Wall.plot(x, y, color="r", label='Line 1')
Wall.plot(x, yy, color="b", label='Line 2')
pyplot.ylabel("Ylabel")
Wall.get_legend_handles_labels()
pyplot.legend()
pyplot.show()
Sidewalk = My_Figure_Window.add_subplot(Sub_Plot_Rows, Sub_Plot_Cols, 2)
Sidewalk.plot(x, y, label='Line 3')
Sidewalk.plot(x, yy, label='Line 4')
pyplot.ylabel("Ylabel")
Sidewalk.get_legend_handles_labels()
pyplot.legend()
pyplot.show()
HeadWall = My_Figure_Window.add_subplot(Sub_Plot_Rows, Sub_Plot_Cols, 3)
HeadWall.plot(x, y, label='Line 5')
HeadWall.plot(x, yy, label='Line 6')
pyplot.ylabel("Ylabel")
HeadWall.get_legend_handles_labels()
pyplot.legend()
pyplot.show()
SideTank = My_Figure_Window.add_subplot(Sub_Plot_Rows, Sub_Plot_Cols, 4)
SideTank.plot(x, y, label='Line 7')
SideTank.plot(x, yy, label='Line 8')
pyplot.ylabel("Ylabel")
SideTank.get_legend_handles_labels()
pyplot.legend()
pyplot.show()
Can anyone just point me in the right direction as to where / how I can share my X axis for all these subplots?
On a side note I have realized that the code pyplot.show()
does not have any affect yet everyone includes it?
Upvotes: 2
Views: 1413
Reputation: 907
There are two main ways to use matplotlib. The first is the pyplot "stateful" API, where you make calls like pyplot.legend()
, and the other is an object-oriented API, where you make calls like `SideTank.get_handles_labels(). Both are fine, but mixing them too much sometimes leads to confusion. This is the problem in your case. For instance,
SideTank.get_legend_handles_labels()
pyplot.legend()
The first call is part of the OO API, the second is the pyplot one. You probably want to use SideTank.legend()
if you're using the OO API, and pyplot.legend()
only if you're using the Pyplot API.
If you want to use the pyplot API:
If you want to use the OO API:
Here are a few ways to accomplish twinned axes:
# This will create a Figure and a list of 4 axes objects. All of those axes objects will have sharex X axes
figure, axes = pyplot.subplots(nrows=4, sharex=True)
axes[0].plot((0, 1), (0, 1))
axes[1].plot((0, 2), (0, 1))
axes[2].plot((0, 0.5), (0, 1))
axes[3].plot((1, 1.5), (0, 1))
# Create a figure
fig = pyplot.figure()
# Use that figure to create a first subplot
ax1 = fig.add_subplot(4, 1, 1)
# Make the following subplots twin that subplot's x axis.
ax2 = fig.add_subplot(4, 1, 2, sharex = ax1)
ax3 = fig.add_subplot(4, 1, 3, sharex = ax1)
ax4 = fig.add_subplot(4, 1, 4, sharex = ax1)
ax1.plot((0, 1), (0, 1))
ax2.plot((0, 2), (0, 1))
ax3.plot((0, 0.5), (0, 1))
ax4.plot((1, 1.5), (0, 1))
If you want to use the pyplot API, it'll look something like this.
# Create a current figure and subplot
pyplot.figure()
pyplot.subplot(4, 1, 1)
# Plot something in the current subplot
pyplot.plot((0, 1), (0, 1))
# short for "get current axes"
# This returns an Axis, so we're dipping into the OO API; it's not uncommon to need to
# do that to do more advanced things, but it's a signal to be careful!
share_handle = pyplot.gca()
# Select a new subplot, and tell it to share its x axis with the first subplot.
pyplot.subplot(4, 1, 2, sharex = share_handle)
pyplot.plot((0, 2), (0, 1))
pyplot.subplot(4, 1, 3, sharex = share_handle)
pyplot.plot((0, 0.5), (0, 1))
pyplot.subplot(4, 1, 4, sharex = share_handle)
pyplot.plot((1, 1.5), (0, 1))
All of these will create something like the following:
On your secondary question, pyplot.show()
displays figures not subplots. Thus, you probably only want to call it once, when you're all done setting up the entire figure.
Upvotes: 0