MulaiMulai sekarang secara gratis

Using a time index to zoom in

When a time-series is represented with a time index, we can use this index for the x-axis when plotting. We can also select a range of dates to zoom in on a particular period within the time-series using pandas' indexing facilities. In this exercise, you will select a portion of a time-series dataset and you will plot that period.

The data to use is stored in a DataFrame called climate_change, which has a time-index with dates of measurements and two data columns: "co2" and "relative_temp".

Latihan ini adalah bagian dari kursus

Introduction to Data Visualization with Matplotlib

Lihat Kursus

Petunjuk latihan

  • Use plt.subplots to create a Figure with one Axes called fig and ax, respectively.
  • Create a variable called seventies that includes all the data between "1970-01-01" and "1979-12-31".
  • Add the data from seventies to the plot: use the DataFrame index for the x value and the "co2" column for the y values.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

import matplotlib.pyplot as plt

# Use plt.subplots to create fig and ax
____

# Create variable seventies with data from "1970-01-01" to "1979-12-31"
seventies = climate_change[____]

# Add the time-series for "co2" data from seventies to the plot
____(____, ____["co2"])

# Show the figure
____
Edit dan Jalankan Kode