以月份为索引的图形数据

时间:2018-10-31 09:13:14

标签: python-3.x pandas matplotlib

我正在努力寻找一个解决方案,在该解决方案中,我将输出一个条形图,其中的列按月分组,并为此数据帧的PN列标记。

分组

            EXT_PRICE            PN
 ENTRY_DATE                         
2017-12-12        0.0     3800702-1
2017-12-21        0.0     3800702-1
2017-12-26   585000.0     3800702-1
2018-01-10   225000.0   737-0010-25
2018-01-16    85000.0     2206400-2
2018-01-17    75000.0    173A0103-5
2018-01-17   565000.0     3800702-1
2018-01-29   565000.0     3800702-1
2018-02-08   150000.0    314-2100-2
2018-02-14   565000.0     3800702-1
2018-02-23   565000.0     3800702-1
2018-02-27   150000.0  737-0010-322
2018-02-28   565000.0     3800702-1
2018-03-05   565000.0     3800702-1
2018-03-06   580000.0     3800702-1
2018-03-12   565000.0     3800702-1
2018-03-13   580000.0     3800702-1
2018-03-22   650000.0     3800702-1
2018-04-03   115000.0    314-2100-2
2018-04-06   585000.0     3800702-1
2018-04-18    80000.0    HG2050AC07
2018-04-19   585000.0     3800702-1
2018-04-19   599900.0     3800702-1
2018-05-03    75000.0   1152426-245
2018-05-22   595000.0     3800702-1
2018-05-23   520000.0     3800702-1
2018-05-25   500000.0     3800702-1
2018-05-25   510000.0     3800702-1
2018-06-07   475000.0     3800702-1
2018-06-07   475000.0     3800702-1
2018-06-15   500000.0     3800702-1
2018-06-19   475000.0     3800702-1
2018-06-21   185000.0  822-1567-102
2018-07-02   185000.0  822-1567-102
2018-07-06   167000.0  822-1567-102
2018-07-09   174000.0  822-1567-102
2018-07-09   475000.0     3800702-1
2018-07-17   475000.0     3800702-1
2018-08-06   500000.0     3800702-1
2018-08-13   115000.0   251A4510-12
2018-08-15   500000.0     3800702-1
2018-08-22   115000.0   251A4510-12
2018-08-23   150000.0   737-0010-25
2018-09-04   150000.0   737-0010-25
2018-09-07   500000.0     3800702-1
2018-09-10   500000.0     3800702-1
2018-09-19   500000.0     3800702-1
2018-09-24   480000.0     3800702-1
2018-10-01   115000.0   251A4510-12
2018-10-01   110000.0    314-2100-2
2018-10-01   120000.0    314-2100-2
2018-10-04   115000.0   251A4510-12
2018-10-19    69000.0   1152426-245

有什么办法吗?

df = pd.read_csv("PO25474.csv", encoding = 'Latin-1')

quotes['ENTRY_DATE'] = quotes['ENTRY_DATE'].astype('datetime64')
quotes['EXT_PRICE'] = quotes['EXT_PRICE'].apply(convert_currency)
quotes = quotes.set_index(['ENTRY_DATE'])
grouped = quotes[[ 'EXT_PRICE', 'PN']].sort_values(['EXT_PRICE'], ascending=False).groupby(lambda x: x.month).head(5)

grouped = grouped.sort_index()

plt.figure()
grouped.plot(kind='bar')

plt.show()

到目前为止,我得到的只是按日期顺序排列的各个列,但我想按月份将它们分组在一起,并用Pn列标记每个列。到目前为止,它根本不会运行。

编辑:我相信聚集的条形图是我希望吐出但仍然失败的术语。

Current Graph output

这是我目前得到的,我希望他们按月分组。

1 个答案:

答案 0 :(得分:0)

尝试一下

    import numpy as np
import matplotlib.pyplot as plt

men_means, men_std = (20, 35, 30, 35, 27), (2, 3, 4, 1, 2)
women_means, women_std = (25, 32, 34, 20, 25), (3, 5, 2, 3, 3)

ind = np.arange(len(men_means))  # the x locations for the groups
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(ind - width/2, men_means, width, yerr=men_std,
                color='SkyBlue', label='Men')
rects2 = ax.bar(ind + width/2, women_means, width, yerr=women_std,
                color='IndianRed', label='Women')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind)
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))
ax.legend()


def autolabel(rects, xpos='center'):
    """
    Attach a text label above each bar in *rects*, displaying its height.

    *xpos* indicates which side to place the text w.r.t. the center of
    the bar. It can be one of the following {'center', 'right', 'left'}.
    """

    xpos = xpos.lower()  # normalize the case of the parameter
    ha = {'center': 'center', 'right': 'left', 'left': 'right'}
    offset = {'center': 0.5, 'right': 0.57, 'left': 0.43}  # x_txt = x + w*off

    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width()*offset[xpos], 1.01*height,
                '{}'.format(height), ha=ha[xpos], va='bottom')


autolabel(rects1, "left")
autolabel(rects2, "right")

plt.show()

阅读matplotlib文档 https://matplotlib.org/gallery/lines_bars_and_markers/barchart.html#sphx-glr-gallery-lines-bars-and-markers-barchart-py