2015-11-03 100 views
2

我有日期列表,我想用python中的matplotlib生成條形圖。如何在matplotlib python中每年生成一個條形圖?

2007-05-06 
2007-05-11 
2007-06-01 
2007-06-04 
2007-06-06 
2007-09-01 
2007-10-06 
2007-11-06 
2007-11-07 
… 

而且我想提供這種兩點式酒吧的char

enter image description here

我可以用這個代碼,但我在尋找更有效的代碼,因爲你可以看到我有多年間2007年和2012年,有時這個範圍可以更寬

def plot(): 
    #--- the two samples --- 
    samples1 = [1, 1, 1, 3, 2, 5, 1, 10, 10, 8] 
    samples2 = [6, 6, 6, 1, 2, 3, 9, 12 ] 
    samples3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 11, 12] 

    N = 12 # number of bins 
    hist1 = np.array([0] * N) 
    hist2 = np.array([0] * N) 
    hist3 = np.array([0] * N) 

    #--- create two histogram. Values of 1 go in Bin 0 --- 
    for x in samples1: 
     hist1[x-1] += 1 
    for x in samples2: 
     hist2[x-1] += 1 
    for x in samples3: 
     hist3[x-1] += 1 

    #--- display the bar-graph ---   
    width = 1 
    p1 = plt.bar(np.arange(0,N)+0.5, hist1, width, color='#9932cc') 
    p2 = plt.bar(np.arange(0,N)+0.5, hist2, width, color='#ffa500', bottom=hist1) 
    p3 = plt.bar(np.arange(0,N)+0.5, hist3, width, color='#d2691e', bottom=hist1+hist2) 
    plt.legend((p1[0], p2[0], p3[0]), ('hist1', 'hist2', 'hist3')) 
    plt.xlabel('Bins') 
    plt.ylabel('Count') 
    #plt.axis([1, 46, 0, 6]) 
    plt.xticks(np.arange(1,N+1)) 
    plt.axis([width/2.0, N+width/2.0, 0, max(hist1+hist2+hist3)]) 
    plt.show() 

你能幫我生成這種圖表!

謝謝

回答

2

兩個地塊都以非常相似的方式產生的,所以我會做的只有第一個。你需要循環的幾個月裏,並獲得堆疊條形圖設定每個月的酒吧的bottom到前幾個月的價值觀的每年累計金額:

import numpy as np 
import matplotlib.pyplot as plt 

months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 
      'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec') 

# Some random data for nyears from minyear 
nyears = 8 
nmonths = len(months) 
minyear = 2005 
monthly_counts = np.random.randint(low=2, high=15, size=(nyears,nmonths)) 

fig, ax = plt.subplots() 
ind = np.arange(nyears) 
width = 0.45 
# Random colors for the months 
c = np.random.rand(nmonths,3,1) 

p = [] 
for imonth in range(nmonths): 
    p.append(ax.bar(ind, monthly_counts[:,imonth], width, 
        bottom=np.sum(monthly_counts[:,:imonth], axis=1), 
        color=c[imonth], alpha=0.8) 
      ) 

# Set x axis ticks and labels 
ax.set_xticks(ind + width/2) 
ax.set_xticklabels([str(minyear+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], months, loc='center left', bbox_to_anchor=(1, 0.5)) 

plt.show() 

enter image description here

+0

怎麼能我展示了這種類型的數據:[['2012','3','22'],['2012','3','27'],['2012','3','30'], ['2012','3','30'],['2012','4','7']] – GeoCom

+0

@Milban請問這是一個新的SO問題:你會得到比我更好的答案編輯成我自己的原始問題。 – xnx

+1

比你,我在這裏問它http://stackoverflow.com/questions/33499081/plot-csv-file-in-matplotlib – GeoCom

3

您可以使用numpyhistogram獲得直接的酒吧格式的數據,這應該是比Python的循環速度更快。作爲基於以上數據小例子,

import numpy as np 
import matplotlib.pyplot as plt 

#--- the two samples --- 
samples1 = [1, 1, 1, 3, 2, 5, 1, 10, 10, 8] 
samples2 = [6, 6, 6, 1, 2, 3, 9, 12 ] 
samples3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 11, 12] 

N = 12 # number of bins 
hist1 = np.array([0] * N) 
hist2 = np.array([0] * N) 
hist3 = np.array([0] * N) 

#--- create two histogram. Values of 1 go in Bin 0 --- 
hist1, n = np.histogram(samples1,N) 
hist2, n = np.histogram(samples2,N) 
hist3, n = np.histogram(samples3,N) 

#--- display the bar-graph ---   
width = 1 
p1 = plt.bar(np.arange(0,N)+0.5, hist1, width, color='#9932cc') 
p2 = plt.bar(np.arange(0,N)+0.5, hist2, width, color='#ffa500', bottom=hist1) 
p3 = plt.bar(np.arange(0,N)+0.5, hist3, width, color='#d2691e', bottom=hist1+hist2) 
plt.legend((p1[0], p2[0], p3[0]), ('2010', '2011', '2012')) 
plt.xlabel('Bins') 
plt.ylabel('Count') 
import datetime 
months = [((datetime.date(2010, i, 1).strftime('%B'))[:3]) for i in range(1,13)] 
plt.xticks(np.arange(1,N+1),months) 
plt.axis([width/2.0, N+width/2.0, 0, max(hist1+hist2+hist3)]) 
plt.show() 
相關問題