Data visualization : how to plot Python Pandas dataframe with different scaled y-axes and legend

Spread the word!

Motivation

Goal of the following code is to flexibly plot more columns of a dataframe with the following characteristics:

  • datetime values is along the x-axis and appropriate spacing is set
  • there is an undefined number of y-axes (more than 1)
  • each y-axes is scaled differently
  • along y-axes all too “long” number will be shortened by using the appropriate suffix (e.g. 200000 becomes 200 k)
  • a (user defined) modified version of the column names is used to label the legend on the plot

The cool thing here is that the user DON’T need to change the code to adapt it to a different number of columns (i.e. y-axes).

Code

First of all, we need to import all necessary libraries and read our .csv file to turn it into a Pandas dataframe:

Our dataframe looks like this:

Now the plot is built by adding axes iteratively for each column of the input dataframe.

Result:

Be the first to comment

Leave a Reply

Your email address will not be published.


*