У меня есть ряд данных панды, которую я хочу построить на основе месяцев, как это: Trouble черчения панды серии
Вот код:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Import text file which holds ImageStack's path
df = pd.read_csv('file.txt', header=None)
df.columns = ['paths']
# Columns 2=Year, 3=Month, 4=Day
df = df.ix[:, 1:4].astype(int)
df.columns = ['year', 'month', 'day']
count = df.groupby(['year', 'month']).count()
count.columns = ['count']
months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')
nmonths = len(months)
fig, ax = plt.subplots()
ind = np.arange(nmonths)
width = 0.45
nyears = len(count.index.levels[0])
p = []
for year in count.index.levels[0]:
df_empty = pd.DataFrame({'months': ind})
monthly_sum = df_empty.join(count.ix[year, :].groupby(level=0)
.sum())['count'].values
counts_until = df_empty.join(count.ix[:year, :].groupby(level=1)
.sum())['count'].values
p.append(ax.bar(ind, monthly_sum, width,
bottom=counts_until, alpha=0.8))
# Set x axis ticks and labels
ax.set_xticks(ind + width/2)
ax.set_xticklabels([months[i] for i in ind])
# Locate legend outside axes plot area
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
ax.legend([pl[0] for pl in p], count.index.levels[0], loc='center left',
bbox_to_anchor=(1, 0.5))
plt.show()
DATA
year month day
0 2007 5 6
1 2007 5 6
2 2007 5 8
3 2007 5 8
4 2007 5 12
5 2007 5 15
6 2007 5 16
7 2007 5 19
8 2007 5 21
9 2007 5 21
10 2007 5 21
11 2007 5 24
Но это не дает мне права. Сложенная гистограмма! Я думаю, проблема может быть от counts_until есть ли кто-нибудь, кто может найти способ решить эту проблему??
'не дает мне правильный столбчатой chart' ... что это дает вам, и почему это не так? – tom