繁体   English   中英

多列 plot Seaborn 和 Matplotlib

[英]Multicolumn plot Seaborn and Matplotlib

我有这个数据表:

Date, City, Sales, PSale1, PSale2

其中 PSale1 和 PSale2 是销售的预测价格。

数据集如下所示:

Date,City,Sales,PSale1,PSale2
01/01,NYC,200,300,178
01/01,SF,300,140,100
02/01,NYC,400,410,33
02/01,SF,42,24,60

I want to plot this data in seaborn, I was working on to plot a graph which will have date on the x-axis, and PSale1 and PSale2 as bars for each city on y-axis, but I am confused how to work with such一个数据。

据我所知 seaborn 不允许在任何轴上绘制两个变量,我应该如何处理这种情况?

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

data = {'Date': ['01/01', '01/01', '02/01', '02/01'],
        'City': ['NYC', 'SF', 'NYC', 'SF'],
        'Sales': [200, 300, 400, 42],
        'PSale1': [300, 140, 410, 24],
        'PSale2': [178, 100, 33, 60]}

df = pd.DataFrame(data)

# convert to long
dfl = df.set_index(['Date', 'City', 'Sales']).stack().reset_index().rename(columns={'level_3': 'pred', 0: 'value'})

# plot
g = sns.FacetGrid(dfl, row='City')
g.map(sns.barplot, 'Date', 'value', 'pred').add_legend()
plt.show()

在此处输入图像描述


  • 都在一个数字
# shape the dataframe
dfc = df.drop(columns=['Sales']).set_index(['Date', 'City']).stack().unstack(level=1)
dfc.columns.name = None

# plot
dfc.plot.bar(stacked=True)
plt.xlabel('Date: Predicted')
plt.show()

在此处输入图像描述

暂无
暂无

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

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