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()
You could transform the dataframe a bit to make the plotting more straight-forward:
With
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:Where
df
is: