Python plot dataframe with multiple lines and two different styles

128 views Asked by At

Assume you have the dataframe df which should be plotted but with two different line styles. Each line with "X_Y" == "Y" should be dashed. I'm wondering if there is a faster and maybe more efficient way than below?

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(
    {
        "Point": (
            "1", "1", "2", "2", "3", "3", "4", "4", "5", "5"
        ),
        "X_Y": (
            "X", "Y", "X", "Y", "X", "Y", "X", "Y", "X", "Y",
        ),
        0: (
            70, 67, 66.7, 68.8, 66.2, 69.5, 68.5, 67.7, 68.8, 67.72,
        ),
        1: (
            69, 68.2, 66.5, 68.1, 66.7, 70, 68.1, 66.7, 66.08, 65.72,
        ),
        2: (
            71, 68, 67.75, 67.8, 67.72, 70.3, 67.6, 66.5, 69.08, 66.72,
        ),
        3: (
            70.5, 67.3, 67.5, 64.8, 68.3, 69.3, 68.6, 68.5, 70.08, 67.72,
        ),
    }
)

print(df)

vals = ["X", "Y"]
styles = ["-", "--"]

plt.figure()
plt.grid(True)
for val, style in zip(vals, styles):
    dff = df.loc[df["X_Y"] == val].drop(["Point", "X_Y"], axis=1).T

    plt.plot(dff, linestyle=style)
    
plt.show() 
1

There are 1 answers

0
mcsoini On

You could transform the dataframe a bit to make the plotting more straight-forward:

fig, ax = plt.subplots(1, 1)

df_unstacked = df.set_index(["X_Y", "Point"]).stack().unstack(["X_Y", "Point"])

df_unstacked["X"].plot(ax=ax, linestyle="-")
df_unstacked["Y"].plot(ax=ax, linestyle="--")

ax.grid(True)
ax.get_legend().remove()
ax.set_xlabel("")

enter image description here

With

print(df_unstacked.sort_index(axis=1))

X_Y       X                                Y                         
Point     1      2      3     4      5     1     2     3     4      5
0      70.0  66.70  66.20  68.5  68.80  67.0  68.8  69.5  67.7  67.72
1      69.0  66.50  66.70  68.1  66.08  68.2  68.1  70.0  66.7  65.72
2      71.0  67.75  67.72  67.6  69.08  68.0  67.8  70.3  66.5  66.72
3      70.5  67.50  68.30  68.6  70.08  67.3  64.8  69.3  68.5  67.72

Alternatively, fully stack the data and use Seaborn, whose plotting functions come with a style parameter (also see this answer). This also gives a nice legend out of the box:

import matplotlib.pyplot as plt
import seaborn as sns

df = df.set_index(["Point", "X_Y"]).rename_axis("x", axis=1).stack().rename("value").reset_index()

fig, ax = plt.subplots(1, 1)
sns.lineplot(data=df, x='x', y='value', hue='Point', style='X_Y', ax=ax)
ax.legend(bbox_to_anchor=(1.04, 0.5), loc="center left")

enter image description here

Where df is:

   Point X_Y  x  value
0      1   X  0  70.00
1      1   X  1  69.00
2      1   X  2  71.00
3      1   X  3  70.50
4      1   Y  0  67.00
5      1   Y  1  68.20
6      1   Y  2  68.00
...