我正在嘗試列印子圖中的頂部專案,但是使用我使用的代碼,我得到了雙列印軸,如何防止這種情況發生,謝謝您的幫助
下面你可以看到代碼和結果圖
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
Cat = ['A', 'B', 'C']
It = ['D', 'E', 'F','G','H','I','J']
n=365
df = pd.DataFrame({'Category': np.random.choice(Cat, n ),
'Item': np.random.choice(It, n ),
'Net sales':np.random.randint(100,500,(n)),
'Date':np.random.choice( pd.date_range('1/1/2021', periods=365,
freq='D'), n, replace=False)})
# Grouping products by sales
prod_sales = pd.DataFrame(df.groupby('Item').sum()['Net sales'])
# Sorting the dataframe in descending order
prod_sales.sort_values(by=['Net sales'], inplace=True, ascending=False)
# fig, ax = plt.subplots(figsize=(20,10))
fig, ax = plt.subplots(5,2,figsize=(20,10))
i=0
for section, group in df.groupby('Item'):
if any(item in section for item in prod_sales[:4].index):
i=i 1
ax = fig.add_subplot(2, 2, i)
group.plot(x='Date', y='Net sales', ax=ax, label=section)

uj5u.com熱心網友回復:
這是我的解決方案,不是最整潔的,多行,但可以完成作業。可以進一步改進或縮短:
Tot = df['Item'].nunique() # number of sublots
Cols = 3 # number of columns in the subplot
Rows = Tot // Cols
Rows = Tot % Cols
Position = range(1,Tot 1)
# Create main figure
df = df.sort_values('Date')
fig = plt.figure(1, figsize = (20, 8))
for k, item in zip(range(Tot), df['Item'].unique()):
ax = fig.add_subplot(Rows, Cols, Position[k])
ax.set_title(item)
ax.plot(df[df['Item'] == item]['Date'], df[df['Item'] == item]['Net sales'])
plt.show()

uj5u.com熱心網友回復:
我處理了上面的輸入,并在我的真實資料上使用了下面的代碼,它對我有用,感謝所有輸入
df=concatenated_df.groupby('Category').resample('M', label='right',closed='left'
, on='Date').sum().reset_index().sort_values(by='Date')
top_cat=df.groupby(['Category']).sum()['Net sales'].nlargest(30)
n=10
fig, axs = plt.subplots(round(n/2),2,figsize=(20,n*2));
fig.subplots_adjust(hspace = 0.5, wspace=0.1);
axs = axs.ravel();
# for ax, (section, group) in zip(axs, df.groupby('Category')):
for ax, (section, group) in zip(axs, df[df.Category.isin(top_cat[:n].index)].groupby('Category')):
group.plot(x='Date', y='Net sales', ax=ax, label=section)
ax.set_title(section )
ax.set_ylim([0, 120000])
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/335153.html
標籤:Python 熊猫 数据框 matplotlib jupyter-笔记本
上一篇:具有不同線條樣式的熊貓繪圖線?
