2014-04-10 38 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

好..这就是我想要的..顺便说一下,我们可以为每个酒吧添加颜色吗? –