同一图上的多条线,带有Pandas图()分组

t2a7ltrp  于 2023-01-28  发布在  其他
关注(0)|答案(3)|浏览(114)

我有一个包含3个数据集的CSV文件,每个数据集对应一条要绘制的线。我使用Pandas plot()分组来对3条线的条目进行分组。这将生成3个单独的图表,但我希望在同一个图表上绘制所有3条线。
CSV:

shop,timestamp,sales
north,2023-01-01,235
north,2023-01-02,147
north,2023-01-03,387
north,2023-01-04,367
north,2023-01-05,197
south,2023-01-01,235
south,2023-01-02,98
south,2023-01-03,435
south,2023-01-04,246
south,2023-01-05,273
east,2023-01-01,197
east,2023-01-02,389
east,2023-01-03,87
east,2023-01-04,179
east,2023-01-05,298

代码(在Jupyter实验室测试):

import pandas as pd

csv = pd.read_csv('./tmp/sample.csv')
csv.timestamp = pd.to_datetime(csv.timestamp)

csv.plot(x='timestamp', by='shop')

这给出了以下内容:

你知道如何在一张图上呈现它们3个吗?

mkshixfv

mkshixfv1#

您可以手动创建子图:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
for name, df in csv.groupby('shop'):
    df.plot(x='timestamp', y='sales', label=name, ax=ax)
ax.set_title('Sales')
plt.show()

u3r8eeie

u3r8eeie2#

[海运替代品(native Pandas.Dataframe.plot answer)]
这是作为备用“* 答案 *”发布的;为了清楚起见而不是将它们混为一谈。
Seaborn根据时间戳(格式为天)绘制每个商店的销售额(由色调指定)。

## import seaborn
import seaborn as sns
## data formater
import matplotlib.dates as mdates

## plot timestamp on horizontal (formated to days), sales on vertical
## with hue set to shop, seaborn plots sales per shop
ax = sns.lineplot(data=df_csv, x='timestamp', y='sales', hue='shop')

## set datetime to days. Ensure this is set AFTER setting ax
ax.xaxis.set_major_locator(locator=mdates.DayLocator())

svdrlsy4

svdrlsy43#

使用ax关键字绘图。df_csv.groupby('shop').plot(x='timestamp', ax=plt.gca())
工作代码如下。

## load libraries
import pandas as pd
import matplotlib.pyplot as plt

## load dataset
df_csv = pd.read_csv('datasets/SO_shop_timestamp_sale.csv')

## check dataset
df_csv.head(3)
df_csv.describe()
df_csv.shape

## ensure data type
df_csv.timestamp = pd.to_datetime(df_csv.timestamp)
df_csv.sales = pd.to_numeric(df_csv.sales)

## Pandas plot of sales against timestamp grouped by shop, using `ax` keyword to subplot.
df_csv.groupby('shop').plot(x='timestamp', ax=plt.gca())

## Pandas plot of timestamp and sales grouped by shop, use `ax` keyword to plot on combined axes.
df_csv.groupby('shop').plot(x='timestamp', kind='kde', ax=plt.gca())

相关问题