Python 在大熊猫身上绘制周期图

Python 在大熊猫身上绘制周期图,python,pandas,matplotlib,Python,Pandas,Matplotlib,让我们假设我有一个带有“日期”列的df来过滤我想要的锦标赛,并且我通过将“日期”列设置为索引来实现。此外,我还有一个功能,可以根据需要配置所有列类型: import pandas as pd df = pd.DataFrame({'Date':['26-12-2018','26-12-2018','27-12-2018','27-12-2018','28-12-2018','28-12-2018'], 'In':['A','B','D','Z','Q','E

让我们假设我有一个带有“日期”列的df来过滤我想要的锦标赛,并且我通过将“日期”列设置为索引来实现。此外,我还有一个功能,可以根据需要配置所有列类型:

import pandas as pd
df = pd.DataFrame({'Date':['26-12-2018','26-12-2018','27-12-2018','27-12-2018','28-12-2018','28-12-2018'],
                   'In':['A','B','D','Z','Q','E'],
                   'Out' : ['Z', 'D', 'F', 'H', 'Z', 'A'],
                   'Score_in' : ['6', '2', '1', '0', '1', '3'], 
                   'Score_out' : ['2','3','0', '1','1','3'],
                   'Place' : ['One','Two','Four', 'Two','Two','One']})

我可以在同一个绘图上绘图吗?因为它是一个网格,例如:

df.groupby('In').Score_in.sum().add(df.groupby('Out').Score_out.sum())
通过将函数参数“day”作为迭代器传递给for cycle for day? 我不明白有多好,比如:

for it in range(26:28:1):

    if it == day:
        ..plot_settings.. f(it)

下面是一段代码,用于构建数据的matplotlib绘图。应该注意的是,据我所知,matplotlib并不是这种绘图最友好的软件包

import matplotlib.pyplot as plt 
import matplotlib.dates as mdates 

df["score"] = pd.to_numeric(df["score"])
df = df.groupby(["Date", "team"]).sum()
df = df.reset_index()

fig, ax = plt.subplots()

groups = df.Date.unique()
num_groups = len(groups)

ind = np.arange(num_groups)    # the x locations for the groups
width = 0.10                   # the width of the bars

days = pd.date_range(df.Date.min(), df.Date.max(), freq = "1D")

rect_list = []
for inx, t in enumerate(df.team.unique()) :
    sub_df = df[df.team == t]
    sub_df = sub_df.set_index("Date").reindex(days)

    x_locations = ind + width * inx
    rects = ax.bar(x_locations, sub_df.score, width, bottom = 0)
    rect_list.append(rects)

ax.legend(rect_list, df.team.unique())
        
ax.set_xticks(ind + width*2)

labels = [pd.to_datetime(x).strftime("%Y-%m-%d") for x in groups]
ax.set_xticklabels(labels)

fig.show()
结果是:

如果您希望每天都在单独的绘图中,可以执行以下操作:

import matplotlib.pyplot as plt 
import matplotlib.dates as mdates 

df["score"] = pd.to_numeric(df["score"])
df = df.groupby(["Date", "team"]).sum()
df = df.reset_index()

days = df.Date.unique()

fig, subplots = plt.subplots(len(days), 1)

all_teams = df.team.unique()

for d, ax in zip(days, subplots):

    sub_df = df[df.Date == d]
    
    sub_df.set_index("team", inplace=True)
    sub_df = sub_df.reindex(all_teams).sort_index().fillna(value = 0)
    
    rects = ax.bar(sub_df.index, sub_df.score, width, bottom = 0)
    ax.set_title(pd.to_datetime(d).strftime("%Y-%m-%d"))

plt.show()
输出:


无关:
范围(26:28:1)
<代码>范围(26,28,1)?或者
[26,27]
?你能解释一下你最后想要什么样的图表(图)吗?X天内每天的总分,如折线图?还有别的吗?我认为更多的是直方图而不是折线图。这将是可行的东西,如hexbin格式?什么直方图?你能在问题中添加所需的输出(即使是餐巾纸背面的草图照片)吗?也许更正确的做法是,在条形图上,x标签上有团队,y标签上有总分,但我想使用for循环,这样我就可以在同一个网格上拥有每天的总分。谢谢你,罗伊!这真的很接近我想要的:有没有一种方法可以让网格图上的每一天都有单独的图,而不是所有的图都在同一个图上?谢谢你的编辑!我还有最后一个疑问:我明白你说的df[“score”]是什么意思,也就是df[“score_in”]+df[“score_out”],但我怎么才能找到你所说的df[“team”]?team是原始的“in”和“out”(A、B、C等)。