2014-04-10 41 views
1

酒吧我想畫條形圖下面數據:如何繪製在python

4 1406575305 4 
4 -220936570 2 
4 2127249516 2 
5 -1047108451 4 
5 767099153 2 
5 1980251728 2 
5 -2015783241 2 
6 -402215764 2 
7 927697904 2 
7 -631487113 2 
7 329714360 2 
7 1905727440 2 
8 1417432814 2 
8 1906874956 2 
8 -1959144411 2 
9 859830686 2 
9 -1575740934 2 
9 -1492701645 2 
9 -539934491 2 
9 -756482330 2 
10 1273377106 2 
10 -540812264 2 
10 318171673 2 

的第一列是x軸和所述第三列是y軸。對於相同的x軸值存在多個數據。例如,

4 1406575305 4 
4 -220936570 2 
4 2127249516 2 

這意味着x軸值爲4三個條和每個條的標記有標記(在中間列中的值)。示例條形圖如下所示: http://matplotlib.org/examples/pylab_examples/barchart_demo.html

我正在使用matplotlib.pyplot和np。謝謝..

回答

0

我跟着你掛的教程,但它是一個有點棘手的不均勻量轉移他們:

import numpy as np 
import matplotlib.pyplot as plt 

x, label, y = np.genfromtxt('tmp.txt', dtype=int, unpack=True) 

ux, uidx, uinv = np.unique(x, return_index=True, return_inverse=True) 
max_width = np.bincount(x).max() 
bar_width = 1/(max_width + 0.5) 

locs = x.astype(float) 
shifted = [] 
for i in range(max_width): 
    where = np.setdiff1d(uidx + i, shifted) 
    locs[where[where<len(locs)]] += i*bar_width 
    shifted = np.concatenate([shifted, where]) 

plt.bar(locs, y, bar_width) 

numbered

如果你願意,你可以與第二貼上標籤列,而不是X:

plt.xticks(locs + bar_width/2, label, rotation=-90) 

labeled

我會把這兩個都作爲練習留給讀者(主要是因爲我不知道你希望他們如何出現)。

+0

好..這就是我想要的..順便說一下,我們可以爲每個酒吧添加顏色嗎? –