简体   繁体   English

Python plot dataframe 有多条线和两个不同的 styles

[英]Python plot dataframe with multiple lines and two different styles

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.假设您有 dataframe df,它应该被绘制,但有两条不同的线 styles。每条带“X_Y”==“Y”的线都应该是虚线。 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:您可以稍微改变 dataframe 以使绘图更直接:

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("")

在此处输入图像描述

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 ).或者,完全堆叠数据并使用 Seaborn,其绘图函数带有style参数(另请参阅此答案)。 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")

在此处输入图像描述

Where df is: df在哪里:

   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
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM