2017-05-16 10 views
1

私はcsvファイルとその仕事からの値ラベルを追加して棒グラフを試しましたが、棒グラフの高さが同じでない理由は何ですか?棒グラフ(高さ)が等しくない

CSVファイル: CPU_5_SEC; CPU_1_MIN; CPU_5_MIN。 27; 17; 16;

コード:

import numpy as np 
import matplotlib.pyplot as plt 

N = 3 
men_std=(0,1,2) 

data = np.loadtxt('show-process-cpu.csv',dtype=bytes, delimiter=';', usecols=(0,1,2)) 
utilization= data[1] 

label = np.loadtxt('show-process-cpu.csv',dtype=bytes, delimiter=';', usecols=(0,1,2)).astype(str) 
my_xticks = label[0] 

ind = np.arange(N) 
width = 0.40 

rects = plt.bar(ind, utilization, width ,men_std,color='r',) 

plt.title("Cpu Utilization\n ('%') ") 
plt.xticks(ind,my_xticks) 

def autolabel(rects): 

    for rect in rects: 
     height = rect.get_height() 
     plt.text(rect.get_x() + rect.get_width()/2,height, 
       '%d' % int(height), 
       ha='center', va='bottom') 

autolabel(rects) 

plt.show() 
+0

あなたは私の答えにあなたのコメントを削除しましたか?私は通知を受けたが、今はそれがなくなった。それがうまくいけば私に教えてください、もしそうであれば、答えを受け入れてください。 –

+0

遅く返事を申し訳ありません:)、私は試して、その仕事、ありがとう:) –

+0

np!私はそれを感謝しますが、私たちは主に、ポスターを褒賞し、問題が解決したことを他の人に知らせる正しい/最善の回答(それを受け入れることによって)(https://meta.stackexchange.com/a/5235)に感謝します。 –

答えて

0

あなたax.bar 2番目の引数が整数の配列である必要があります。私はまた、men_std=(0,1,2)引数と定義を削除しました。これにより、バーが異なる高さにプロットされるためです。

import numpy as np 
import matplotlib.pyplot as plt 

N = 3 

data = np.loadtxt('show-process-cpu.csv',dtype=bytes, delimiter=';', usecols=(0,1,2)) 

my_xticks = data[0] 
utilization = data[1] 
utilization_int = [int(x) for x in utilization] 

ind = np.arange(N) 
width = 0.40 

fig, ax = plt.subplots() 
rects1 = ax.bar(ind, utilization_int, width,color='r',) 

ax.set_title("Cpu Utilization\n ('%') ") 
ax.set_xticks(ind) 
ax.set_xticklabels(my_xticks) 

def autolabel(rects): 
    for rect in rects: 
     height = rect.get_height() 
     ax.text(rect.get_x() + rect.get_width()/2,height, 
       '%d' % int(height), 
       ha='center', va='bottom') 

autolabel(rects1) 
plt.show() 

enter image description here

関連する問題