2 Plots In Same Figure Python
Sometimes, as part of a quick exploratory data analysis, you may want to make a single plot containing two variables with different scales.
To plot multiple graphs on the same figure you will have to do: from numpy import. import math import matplotlib.pyplot as plt t = linspace(0, 2.math.pi, 400) a = sin(t) b = cos(t) c = a + b plt.plot(t, a, 'r') # plotting t, a separately plt.plot(t, b, 'b') # plotting t, b separately plt.plot(t, c, 'g') # plotting t, c separately plt.show. I have two different fits files with different shapes (i.e. (310, 256, 256) & (560, 4, 786432)). How do i make two scatter plots to compare them using python? Making multiple Plots in the same figure using plot superimposition with separate plot commands Plots with Common X-axis but different Y-axis: Using twinx Plots with common Y-axis and different X-axis using twiny Plugin and Extension Classes.
One of the options is to make a single plot with two different y-axis, such that the y-axis on the left is for one variable and the y-axis on the right is for the y-variable.
If you try to plot the two variables on a same plot without having two different y-axis, the plot would not really make sense.
If the variables have very different scales, you’ll want to make sure that you plot them in different twin Axes objects. These objects can share one axis (for example, the time, or x-axis) while not sharing the other (the y-axis).
To create a twin Axes object that shares the x-axis, we use the twinx method.
Let us import Pandas.
We will use gapminder data from Carpentries to make the plot with two different y-axis on the same plot.
Let us subset gapminder data by using Pandas query() function to filter for rows with United States.
We are interested in making a plot of how lifeExp & gdpPercap changes over the years. The variable on x-axis is year and on y-axis we are interested in lifeExp & gdpPercap.
Both lifeExp and gdpPercap have different ranges. lifeExp values are below 100 and gdpPercap values are in thousands.
Naively, let us plot both on the same plot with a single y-axis.
We can immediately see that this is a bad idea. The line for lifeExp over years is flat and really low. We don’t see any variation in it because of the scale of gdpPercap values.
One of the solutions is to make the plot with two different y-axes. The way to make a plot with two different y-axis is to use two different axes objects with the help of twinx() function.
We first create figure and axis objects and make a first plot. In this example, we plot year vs lifeExp. And we also set the x and y-axis labels by updating the axis object.
Next we use twinx() function to create the second axis object “ax2”. Now we use the second axis object “ax2” to make plot of the second y-axis variable and update their labels.
Then we can display the plot with plt.show() as before.
Now we have what we wanted. A plot with with different y-axis made with twinx in matplotlib. This definitely help us understand the relationship of the two variables against another. We can see that both lifeExp and gdpPerCap have increased over the years.
Although a plot with two y-axis does help see the pattern, personally I feel this is bit cumbersome. A better solution to use the idea of “small multiples”, two subplots with same x-axis. We will see an example of that soon.
Related posts:
In this article, we show how to add multiple axes to a figure in matplotlib with Python.
So with matplotlib, the heart of it is to create a figure.
On this figure, you can populate it with all different types of data, including axes, a graph plot, a geometric shape, etc.
We don't have to simply populate the figure with one X and Y axes.
We can add multiple axes to the figure.
This is shown in the following code below.
So the first thing we have to do is import matplotlib. We do this with the line, import matplotlib.pyplot as plt
We then create a variable fig, and set it equal to, plt.figure()
This creates a figure object, which of course is initially empty, because we haven't populated it with anything.
So we have a figure object stored in the variable, fig, that is empty.
We then add axes to this figure.
Our first axes is the bigger of the axes, axes1.
The line, fig.add_axes([0.1,0.1,0.8,0.8]), makes the figure 10% from the left of the figure, 10% from the bottom of the figure, 80% width of the figure, and 80% height of the figure.
From left to the right, the values of the add_axes() function is [left, bottom, width, height]
We then create a second axes, axes2.
This second axes is created with the line, axes2= fig.add_axes([0.17,0.5,0.4,0.3])
This second axes is created and placed 17% from the left of the figure, 50% from the bottom of the figure, takes up 40% of the width of the figure, and takes up 30% of the height ofthe figure. This places this axes within the larger axes.
The x axis is composed of the values 1-12.
The y axis is compsed of the values 12-144.
We then plot graphs of the x and y values on each of the axes.
To show this figure object, we use the line, fig.show()
This works if you're using a python IDE other than jupyter notebooks. If you are using jupyter notebooks, then you would not use, plt.show(). Instead you would specify in the code right after importing matplotlib, %matplotlib inline
This line allows the figure of a graph to be shown with jupyter notebooks.
After running the following code above, we get the following figure with axes shown in the image below.
Plot Line In Python
So now you see a figure object with axes added to it.
And this is how to add axes to an figure object in matplotlib with Python.
Related Resources
How to Randomly Select From or Shuffle a List in Python