# Plotting two variables

If you want to plot two time-series variables that were recorded at the same times, you can add both of them to the same subplot.

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.

In this exercise, you'll have access to a DataFrame that has the `climate_change`

data loaded into it. This DataFrame was loaded with the `"date"`

column set as a `DateTimeIndex`

, and it has a column called `"co2"`

with carbon dioxide measurements and a column called `"relative_temp"`

with temperature measurements.

This is a part of the course

## “Introduction to Data Visualization with Matplotlib”

### Exercise instructions

- Use
`plt.subplots`

to create a Figure and Axes objects called`fig`

and`ax`

, respectively. - Plot the carbon dioxide variable in blue using the Axes
`plot`

method. - Use the Axes
`twinx`

method to create a twin Axes that shares the x-axis. - Plot the relative temperature variable in red on the twin Axes using its
`plot`

method.

### Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

```
import matplotlib.pyplot as plt
# Initalize a Figure and Axes
____
# Plot the CO2 variable in blue
ax.plot(____, ____, color=____)
# Create a twin Axes that shares the x-axis
ax2 = ____
# Plot the relative temperature in red
____.plot(____, ____, color=____)
plt.show()
```

This exercise is part of the course

## Introduction to Data Visualization with Matplotlib

Learn how to create, customize, and share data visualizations using Matplotlib.

Time series data is data that is recorded. Visualizing this type of data helps clarify trends and illuminates relationships between data.

Exercise 1: Plotting time-series dataExercise 2: Read data with a time indexExercise 3: Plot time-series dataExercise 4: Using a time index to zoom inExercise 5: Plotting time-series with different variablesExercise 6: Plotting two variablesExercise 7: Defining a function that plots time-series dataExercise 8: Using a plotting functionExercise 9: Annotating time-series dataExercise 10: Annotating a plot of time-series dataExercise 11: Plotting time-series: putting it all together### What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.