我有一個(gè)時(shí)間索引表,該表的BLIP列只有兩個(gè)值“ XX”和“ YY”。目的是顯示x軸以下的計(jì)數(shù)“ XX”和“ YY”,其中“ YY”。我正在嘗試使用Wes McKenney關(guān)于數(shù)據(jù)分析的書(我認(rèn)為是第26頁(yè))中的代碼從熊貓表中創(chuàng)建正確的數(shù)據(jù)結(jié)構(gòu):df = base_df.drop(columns=dropcols).set_index('Created')group = ['f2','BLIP']df0 = df_minus.groupby(group)agg_counts = df0.size().unstack().fillna(0)indexer = agg_counts.sum(1).argsort()count_subset = agg_counts.take(indexer).copy()table = count_subset.groupby('BLIP').resample('MS').count().unstack('BLIP')['BLIP']chart = table.plot.bar(title = chart_title, x=None, color = ['green', 'red', 'grey']);線agg_counts = df0.size().unstack().fillna(0) 導(dǎo)致以下錯(cuò)誤:TypeError: 'numpy.int32' object is not callable我在這里找到了一段代碼的寶石,但是找不到解密它的文檔。data['values'].plot(kind='bar', color=data.positive.map({True: 'g', False: 'r'}))這似乎是非常簡(jiǎn)單的,但是我對(duì)此頗為關(guān)注。大熊貓表格式類似于create_date f1 f2 f3 BLIP f5...dt_stamp X Y Z XX K1dt_stamp S R Y YY K3dt_stamp P P T XX K1等等。我嘗試過杰西的建議df_plus =df[df['BLIP']=='XX']df_minus=df[df['BLIP']=='YY']ax = plt.axes()ax.bar(df_plus.index, df_plus['BLIP'], width=0.4, color='g')ax.bar(df_neg.index, df_minus['BLIP'], width=0.4, color='r')ax.autoscale()plt.show()這導(dǎo)致ValueError: shape mismatch: objects cannot be broadcast to a single shape整體解決方案df = base_dfplt.clf()fig = plt.figure()width = 8height = 6fig.set_size_inches(width, height)chart_title = 'YTD CR Trend Summary'df_plus =df[df['BLIP'] == 'XX']df_minus=df[df['BLIP']== 'IYY']p = df_plus.resample('MS').count()['BLIP'].fillna(0)n = df_minus.resample('MS').count()['BLIP'].apply(lambda x: int(-x)) print(chart_title, len(df), p.sum(), n.sum())plt.clf()fig = plt.figure()# ax = fig.add_subplot(1, 1, 1)ax = plt.axes(label=chart_title) #label suppresses warningif p.sum() != False: ax.bar(p.index, p, width=10, color='g') if n.sum() != False: ax.bar(n.index, n, width=10, color='r')plt.suptitle(chart_title, fontsize=11)filename = f'{graph_images_dir}{chart_title}.png'print(f'Saving {filename}')plt.savefig(filename, bbox_inches='tight', pad_inches=0.5, dpi=200)plt.show()
添加回答
舉報(bào)
0/150
提交
取消