LoslegenKostenlos loslegen

Add vertical and horizontal markers

Additional annotations can help further emphasize specific observations or events. Here, you will learn how to highlight significant events by adding markers at specific timestamps of your time series plot. The matplotlib library makes it possible to draw vertical and horizontal lines to identify particular dates.

Recall that the index of the discoveries DataFrame are of the datetime type, so the x-axis values of a plot will also contain dates, and it is possible to directly input a date when annotating your plots with vertical lines. For example, a vertical line at January 1, 1945 can be added to your plot by using the command:

ax.axvline('1945-01-01', linestyle='--')

Diese Übung ist Teil des Kurses

Visualizing Time Series Data in Python

Kurs anzeigen

Anleitung zur Übung

  • Add a red vertical line at the date January 1, 1939 using the .axvline() method.
  • Add a green horizontal line at the y-axis value 4 using the .axhline() method.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# Plot your the discoveries time series
ax = discoveries.plot(color='blue', fontsize=6)

# Add a red vertical line
ax.____(____, color=____, linestyle='--')

# Add a green horizontal line
ax.____(____, color=____, linestyle='--')

plt.show()
Code bearbeiten und ausführen