Skip to content
Advertisement

How to plot a wide dataframe with colors and linestyles based on different columns

Here’s a dataframe of mine:

JavaScript

Output:

JavaScript

I need to plot val1 and val2 over time, in different colors (say green and red). There are also two classes A and B, and I’d like to plot the two classes in different line types (solid and dashed). So if class is A, then val1 might be solid green in the plot, and if the class is B, then val1 might be dashed green in the plot. If class is B, then val2 might be solid red in the plot, and if the class is B, then val2 might be dashed red in the plot.

But I got a problem with the time (x-axis) that I need to resolve. First of all, the time is in different columns (year and month) and there are different amount of rows for the two classes. In the data above, class B doesn’t start till Nov. of 2020.

My attempt to resolve this is to create new index using the year and month:

JavaScript

enter image description here

But this creates non-ideal tick labels on the x-axis (which I suppose I can rename later). While it differentiates the two classes, it doesn’t do so in the way I want. Nor do I know how to add more columns to the plot. Please advise. Thanks

Advertisement

Answer

  1. Combine the 'year' and 'month' column to create a column with a datetime dtype.
  2. pandas.DataFrame.melt is used to pivot the DataFrame from a wide to long format
  3. Plot using seaborn.relplot, which is a figure level plot, to simplify setting the height and width of the figure.
    • Similar to seaborn.lineplot
    • Specify hue and style for color and linestyle, respectively.
  4. Use mdates to provide a nice format to the x-axis. Remove if not needed.
  • Tested with pandas 1.2.4, seaborn 0.11.1, and matplotlib 3.4.2.

Imports and Transform DataFrame

JavaScript

seaborn.relplot

JavaScript

enter image description here

seaborn.lineplot

JavaScript

enter image description here

Melted df

JavaScript
Advertisement