Python Forum

Full Version: Sharing X Axis in Sub plots
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello community

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 realised that the code pyplot.show() does not have any affect yet everyone includes it?

Thank you.
I don't understand what you mean by "share my x axis". Are you trying to figure out how to have multiple plots in one window? All you need to do is get rid of all but the last "show()" commands