Member-only story

Python Plot with different Y-Axes

Sometimes it is useful to display two data sets in the same plot, even if they have a different Y-axis. This article is a simple step-by-step guide on how to do it.

Benjamin Ja
3 min readNov 4, 2023

As always, the required modules and libraries have to be installed or imported.To create this diagram, the library Matplotlib is used and imported as plt. Matplotlib can be used to visualise data sets in Python.

import matplotlib.pyplot as plt

In my example I visualise the quarterly net income of the Austrian energy provider Verbund together with its stock price.

In this chart, the share price (in orange) is plotted together with the quarterly net profit (dark grey).

As a first step the figure and the size of the plot is set. The numbers 15 and 8 refer to the length and width of the figure. You can change them to a shape that is suitable for your project.

# Create a new figure
fig, ax1 = plt.subplots(figsize=(15,8))

Then the first data set for y-axis 1 (the left side) is inserted. I use bar charts to visualise the quarterly net income. Therefore, the command ax1.bar(.) is used. If you want your data to be visualised by a continous line, you can use ax1.plot(…).

# Plot the Net Income
ax1.bar(VERNIQ['2014-12':'2023-06'].index, VERNIQ['2014-12':'2023-06']['VER.VIEUR_GP']…

--

--

Benjamin Ja
Benjamin Ja

Written by Benjamin Ja

Finance | Economics | Politics | Statistics

No responses yet