Python 具有描述中提到的所有子地块的大地块

Python 具有描述中提到的所有子地块的大地块,python,pandas,Python,Pandas,请帮助在一次拍摄中生成此类图形。这是分析所必需的。需要条形图。这种输出的结构真的会有帮助 Alphabet year month month_name Quantity A 2019 4 April 1 A 2019 5 May 15 A 2019 6 June 23 A 2019 7 July 23.17647059 B 2019 4 April 2 B 2019 5 Ma

请帮助在一次拍摄中生成此类图形。这是分析所必需的。需要条形图。这种输出的结构真的会有帮助

Alphabet    year    month   month_name  Quantity
A   2019    4   April   1
A   2019    5   May 15
A   2019    6   June    23
A   2019    7   July    23.17647059
B   2019    4   April   2
B   2019    5   May 16
B   2019    6   June    24
B   2019    7   July    23.60784314
C   2019    4   April   3
C   2019    5   May 17
C   2019    6   June    25
C   2019    7   July    24.03921569
D   2019    4   April   4
D   2019    5   May 18
D   2019    6   June    26
D   2019    7   July    24.47058824
E   2019    4   April   5
E   2019    5   May 19
E   2019    6   June    27
F   2019    4   April   6
F   2019    5   May 20
F   2019    6   June    28
G   2019    4   April   7
G   2019    5   May 21
G   2019    6   June    0
H   2019    4   April   8
H   2019    5   May 22
I   2019    4   April   9
J   2019    4   April   10
K   2019    4   April   11
L   2019    4   April   12
M   2019    4   April   13
N   2019    4   April   14

---------------------------------------------------------------------------
索引器回溯(最后一次最近调用)
在里面
17个月,以cols表示:
18掩码=(df.Alphabet==字母)和(df.month_name==月份)
--->19 df[mask]。绘图(x='year',y='Quantity',kind='bar',ax=ax[count])
20 ax[计数]。设置标签(字母)
21 ax[计数]。设置标题(月)
C:\ProgramData\Anaconda3\lib\site packages\pandas\plotting\\u core.py在调用中(self,*args,**kwargs)
845 data.columns=标签名称
846
-->847返回plot_backend.plot(数据,种类=种类,**kwargs)
848
849 uuuu调用uuuu.uuuuu文档uuuuu=uuuuuuu文档__
C:\ProgramData\Anaconda3\lib\site packages\pandas\plotting\\u matplotlib\\uu init\uuuuu.py在plot中(数据,种类,**kwargs)
59 kwargs[“ax”]=getattr(ax,“left_ax”,ax)
60绘图对象=绘图类别[种类](数据,**kwargs)
--->61绘图对象生成()
62标绘对象图()
63返回图\对象结果
C:\ProgramData\Anaconda3\lib\site packages\pandas\plotting\\u matplotlib\core.py在generate(self)中
263自绘制图()
264自我添加表()
-->265自成传奇()
266自我装饰子地块()
267
C:\ProgramData\Anaconda3\lib\site packages\pandas\plotting\\u matplotlib\core.py in\u make\u legend(self)
570
571如果透镜(手柄)>0:
-->572轴图例(手柄、标签、loc=“最佳”,标题=标题)
573
574 elif self.subplot和self.legend:
图例中的C:\ProgramData\Anaconda3\lib\site packages\matplotlib\axes\\u axes.py(self,*args,**kwargs)
404如果len(额外参数):
405 raise TypeError('图例仅接受两个非关键字参数')
-->406 self.legend u=mlegend.legend(self、手柄、标签、**kwargs)
407 self.legend.\u remove\u方法=self.\u remove\u legend
408返回self.legend_
C:\ProgramData\Anaconda3\lib\site packages\matplotlib\legend.py in\uuu init__(self、parent、handle、label、loc、numpoints、markerscale、markerfirst、scatterpoints、scatteryoffset、prop、fontsize、borderpad、labelspace、handlelength、handlelight、handleextpad、borderaxespad、columnspacing、ncol、mode、fancybox、shadow、title、title_fontsize、framealpha、edeColor、facecolor、bbox_to_锚定、bbox_变换、frameon、h)安德勒(图)
573
574#带空渲染器的init
-->575 self.\u init\u图例\u框(手柄、标签、标记优先)
576
577#如果阴影已激活,则使用framealpha(如果未激活)
C:\ProgramData\Anaconda3\lib\site packages\matplotlib\legend.py在_init_legend_框中(self、句柄、标签、markerfirst)
831#原创艺术家/手柄。
832句柄列表。附加(句柄。图例)艺术家(自我,原始句柄,
-->833(尺寸,把手盒)
834手柄和标签。附加((手柄盒,文本框))
835
C:\ProgramData\Anaconda3\lib\site packages\matplotlib\legend\u handler.py在legend\u美工中(self、legend、orig\u handle、fontsize、handlebox)
113艺术家=自我。创建艺术家(图例、原始手柄、,
114 xdescent、ydescent、宽度、高度、,
-->115 fontsize,车把箱。get_transform())
116
117#创建艺术家将返回艺术家列表。
C:\ProgramData\Anaconda3\lib\site packages\matplotlib\legend\u handler.py在create\u美工程序中(self、legend、orig\u handle、xdescent、ydescent、width、height、fontsize、trans)
297 p=self.\u创建\u补丁(图例、原始手柄、,
298 xdescent、ydescent、宽度、高度、字体大小)
-->299自我更新道具(p,原始手柄,图例)
300 p.set_变换(trans)
301返回[p]
C:\ProgramData\Anaconda3\lib\site packages\matplotlib\legend\u handler.py在update\u prop中(self、legend\u handle、orig\u handle、legend)
70 def更新道具(自身、图例手柄、原始手柄、图例):
71
--->72自我更新道具(图例手柄、原始手柄)
73
74图例。\套装\艺术家\道具(图例\手柄)
C:\ProgramData\Anaconda3\lib\site packages\matplotlib\legend\u handler.py in\u update\u prop(self、legend\u handle、orig\u handle)
63自我。默认更新属性(图例句柄、原始句柄)
64其他:
--->65自更新属性函数(图例句柄、原始句柄)
66
67定义默认更新属性(自身、图例句柄、原始句柄):
C:\ProgramData\Anaconda3\lib\site packages\matplotlib\legend\u handler.py在\u first\u child(tgt,src)的更新\u中
36
37来自第一个子项(tgt、src)的def更新:
--->38 tgt.update_from(src.get_children()[0])
39
40
索引器:列表索引超出范围
输出应如所附图片链接中所述。它应以数量为基础


[图形的输出--]

这可能就是您要查找的内容,请注意,您可能需要修改
子图\u adjust
参数以获得正确的边距,并修改
figsize
以正确的大小显示图像:

rows = df.Alphabet.unique()
cols = df.month_name.unique()

fig, ax = plt.subplots(rows.shape[0],cols.shape[0], 
                       sharex=True, sharey=True, figsize =(16,16))
ax = ax.reshape(-1)

count = 0
for letter in rows:
    for month in cols:
        mask = (df.Alphabet == letter) & (df.month_name == month)
        ax[count].set_ylabel(letter)
        ax[count].set_title(month)
        if (mask.sum() != 0):
            df[mask].plot(x='year', y='Quantity', kind='bar', ax=ax[count])
        count+=1


plt.subplots_adjust(left=None, top=2, right=None)

显示您到目前为止尝试过的示例代码。有很多图形,x轴和y轴的不同组合,不同类型的图形(条形图、线形图、直方图),请提供您想要的更多信息。嗨,它们应该是条形图…我假设x轴是年,y轴是数量,对吗?x轴-“月-年”.Ex:Apr-2019.Y-数量,这太棒了,但我遇到了一个错误。请告诉我哪里出了问题。我已经更新了描述中的代码。我可以ping Y吗
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-12-11797c631676> in <module>
     17     for month in cols:
     18         mask = (df.Alphabet == letter) & (df.month_name == month)
---> 19         df[mask].plot(x='year', y='Quantity', kind='bar', ax=ax[count])
     20         ax[count].set_ylabel(letter)
     21         ax[count].set_title(month)

C:\ProgramData\Anaconda3\lib\site-packages\pandas\plotting\_core.py in __call__(self, *args, **kwargs)
    845                     data.columns = label_name
    846 
--> 847         return plot_backend.plot(data, kind=kind, **kwargs)
    848 
    849     __call__.__doc__ = __doc__

C:\ProgramData\Anaconda3\lib\site-packages\pandas\plotting\_matplotlib\__init__.py in plot(data, kind, **kwargs)
     59             kwargs["ax"] = getattr(ax, "left_ax", ax)
     60     plot_obj = PLOT_CLASSES[kind](data, **kwargs)
---> 61     plot_obj.generate()
     62     plot_obj.draw()
     63     return plot_obj.result

C:\ProgramData\Anaconda3\lib\site-packages\pandas\plotting\_matplotlib\core.py in generate(self)
    263         self._make_plot()
    264         self._add_table()
--> 265         self._make_legend()
    266         self._adorn_subplots()
    267 

C:\ProgramData\Anaconda3\lib\site-packages\pandas\plotting\_matplotlib\core.py in _make_legend(self)
    570 
    571             if len(handles) > 0:
--> 572                 ax.legend(handles, labels, loc="best", title=title)
    573 
    574         elif self.subplots and self.legend:

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in legend(self, *args, **kwargs)
    404         if len(extra_args):
    405             raise TypeError('legend only accepts two non-keyword arguments')
--> 406         self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
    407         self.legend_._remove_method = self._remove_legend
    408         return self.legend_

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\legend.py in __init__(self, parent, handles, labels, loc, numpoints, markerscale, markerfirst, scatterpoints, scatteryoffsets, prop, fontsize, borderpad, labelspacing, handlelength, handleheight, handletextpad, borderaxespad, columnspacing, ncol, mode, fancybox, shadow, title, title_fontsize, framealpha, edgecolor, facecolor, bbox_to_anchor, bbox_transform, frameon, handler_map)
    573 
    574         # init with null renderer
--> 575         self._init_legend_box(handles, labels, markerfirst)
    576 
    577         # If shadow is activated use framealpha if not

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\legend.py in _init_legend_box(self, handles, labels, markerfirst)
    831                 # original artist/handle.
    832                 handle_list.append(handler.legend_artist(self, orig_handle,
--> 833                                                          fontsize, handlebox))
    834                 handles_and_labels.append((handlebox, textbox))
    835 

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\legend_handler.py in legend_artist(self, legend, orig_handle, fontsize, handlebox)
    113         artists = self.create_artists(legend, orig_handle,
    114                                       xdescent, ydescent, width, height,
--> 115                                       fontsize, handlebox.get_transform())
    116 
    117         # create_artists will return a list of artists.

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\legend_handler.py in create_artists(self, legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans)
    297         p = self._create_patch(legend, orig_handle,
    298                                xdescent, ydescent, width, height, fontsize)
--> 299         self.update_prop(p, orig_handle, legend)
    300         p.set_transform(trans)
    301         return [p]

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\legend_handler.py in update_prop(self, legend_handle, orig_handle, legend)
     70     def update_prop(self, legend_handle, orig_handle, legend):
     71 
---> 72         self._update_prop(legend_handle, orig_handle)
     73 
     74         legend._set_artist_props(legend_handle)

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\legend_handler.py in _update_prop(self, legend_handle, orig_handle)
     63             self._default_update_prop(legend_handle, orig_handle)
     64         else:
---> 65             self._update_prop_func(legend_handle, orig_handle)
     66 
     67     def _default_update_prop(self, legend_handle, orig_handle):

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\legend_handler.py in update_from_first_child(tgt, src)
     36 
     37 def update_from_first_child(tgt, src):
---> 38     tgt.update_from(src.get_children()[0])
     39 
     40 

IndexError: list index out of range

rows = df.Alphabet.unique()
cols = df.month_name.unique()

fig, ax = plt.subplots(rows.shape[0],cols.shape[0], 
                       sharex=True, sharey=True, figsize =(16,16))
ax = ax.reshape(-1)

count = 0
for letter in rows:
    for month in cols:
        mask = (df.Alphabet == letter) & (df.month_name == month)
        ax[count].set_ylabel(letter)
        ax[count].set_title(month)
        if (mask.sum() != 0):
            df[mask].plot(x='year', y='Quantity', kind='bar', ax=ax[count])
        count+=1


plt.subplots_adjust(left=None, top=2, right=None)