Separating Primary and Secondary Axes in Graphs: A Step-by-Step Guide
In data visualization, graphs are an essential tool to represent relationships between different variables. Sometimes, it becomes necessary to represent two quantitative variables on the same graph but with different scales. In such cases, we can use separate axes for each variable, known as primary and secondary axes. In this article, we will discuss how to create graphs with primary and secondary axes using Python and Matplotlib.
Prerequisites
Before we begin, make sure you have the following:
- Python installed on your system
- Matplotlib library installed
You can install Matplotlib using pip:
pip install matplotlib
Creating a Simple Line Plot with Primary and Secondary Axes
Let's start by creating a simple line plot with primary and secondary axes using Python and Matplotlib.
Code
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
y2 = np.cos(x)
fig, ax1 = plt.subplots()
Plot the primary axis
ax1.plot(x, y, 'b-', label='Primary Axis')
ax1.set_xlabel('X-axis')
ax1.set_ylabel('Primary Axis')
ax1.legend(loc='best')
Create a secondary axis
ax2 = ax1.secondary_xaxis('top', functions=[np.linspace(0, 2 * np.pi, 100)])
ax2.set_xlabel('Secondary Axis')
ax2.tick_params(axis='x', labelsize=10)
ax2.xaxis.set_ticklabels([])
Plot the secondary axis
ax1.plot(np.pi * x, y2, 'r-', label='Secondary Axis')
ax1.legend(loc='best')
plt.show()
Understanding the Code
In the code above, we first import the necessary libraries and create some data. We then create a figure and an axis using the subplots function. We plot the primary axis using the plot function and label it using set_xlabel and set_ylabel. To add a secondary axis, we use the secondary_xaxis function and pass the 'top' location and a list of functions representing the secondary axis data. We then plot the secondary axis using the same plot function and label it using set_xlabel and tick_params.
Customizing the Appearance of Primary and Secondary Axes
Sometimes, we may want to customize the appearance of primary and secondary axes. Matplotlib provides several options for this.
Code
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
y2 = np.cos(x)
fig, ax1 = plt.subplots()
Plot the primary axis
ax1.plot(x, y, 'b-', label='Primary Axis')
ax1.set_xlabel('X-axis')
ax1.set_ylabel('Primary Axis')
ax1.tick_params(axis='y', labelsize=12)
ax1.legend(loc='best')
Create a secondary axis
ax2 = ax1.secondary_xaxis('top', functions=[np.linspace(0, 2 * np.pi, 100)])
ax2.set_xlabel('Secondary Axis')
ax2.tick_params(axis='x', labelsize=10)
ax2.xaxis.set_ticklabels([])
ax2.xaxis.set_tick_params(length=5, width=1.5, which='both')
ax2.xaxis.set_ticks_position('top')
ax2.xaxis.offsetText(0, 35)
Plot the secondary axis
ax1.plot(np.pi * x, y2, 'r-', label='Secondary Axis')
ax1.legend(loc='best')
plt.show()
In this article, we discussed how to create graphs with primary and secondary axes using Python and Matplotlib. We covered the basics of creating a simple line plot with primary and secondary axes and customizing their appearance. By following the steps outlined in this article, you will be able to create informative and visually appealing graphs that effectively represent the relationships between multiple variables.