2017-08-01 10 views
0

CSVファイルからドラッグしている2つのリストに基づいて棒グラフを作成しようとしています。XY棒グラフdatanot対応するmatplotlib

これらはしかし、Xの値が並んでいるように見えるない2つのXとして定義リストとY

[42127L, 42129L, 44161L, 44166L, 44167L, 44168L, 44169L, 44170L] 
[21.633899998, 19.125503355699998, 19.9757477769, 20.5028595637, 20.4502863092, 18.4269712278, 20.4741833509, 19.2441994027] 

です。

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 

df = pd.read_csv("N:\\Wild_Pig_Project\\\Mean_temp.csv", names = ['Device_ID', 'Mean']) 

y = df.Mean.tolist() 
x = df.Device_ID.tolist() 

fig, ax = plt.subplots() 
width = 50 
ax.set_xticklabels(x) 
ax.set_yticklabels(y) 
ax.bar(x, y, width, color='Red') 
plt.show() 

Graph

答えて

3

私はあなたのコードを変更しようとした:

import numpy as np 
import matplotlib.pyplot as plt 

x = ['42127L', '42129L', '44161L', '44166L', '44167L', '44168L', '44169L', '44170L'] 
y = [21.633899998, 19.125503355699998, 19.9757477769, 20.5028595637, 20.4502863092, 18.4269712278, 20.4741833509, 19.2441994027] 

fig, ax = plt.subplots() 

ax.bar([idx for idx in range(len(x))], y, color='Red') 
ax.set_xticks([idx+0.5 for idx in range(len(x))]) 
ax.set_xticklabels(x, rotation=35, ha='right', size=10) 
fig.tight_layout() 
plt.show() 

重要なポイントは以下のとおりです。

  1. あなたがの位置についてxticks(yticks)を設定する必要があります。あなたのxtick(ytick)ラベル。
  2. 'bar()'の最初の引数は、各バーの位置です。

私はあなたのyticksのカスタマイズされたラベルを使用したい理由は、私はあなたがbtw異なるデバイスの処理時間の比較を示したいと思います。 これはあなたに合理的な結果を与えるはずです。

+0

の両方が意味をなさない!ありがとう –

2

xtick自体をxとxticklabelsに設定してみることもできます。ただし、xのデータは均等に配置されておらず、バーが非常に近くに集まったグラフが表示されます(スクリーンショットに表示されているものに似ています)。

等間隔の配列を作成し、これを使って棒グラフをプロットし、目盛りラベルをxに設定することができます。以下

例:次の図を生成

import numpy as np 
import matplotlib.pyplot as plt 

x = [42127, 42129, 44161, 44166, 44167, 44168, 44169, 44170] 
y = [21.633899998, 19.125503355699998, 19.9757477769, 20.5028595637, 20.4502863092, 18.4269712278, 20.4741833509, 19.2441994027] 
ticks = np.arange(1,9,1) # can replace 9 with len(y) to be a more general solution 

fig, ax = plt.subplots() 
width = 0.5 

ax.set_xticks(ticks) 
ax.set_xticklabels(x) 
ax.set_yticklabels(y) 

ax.bar(ticks, y, width, color='Red') 

plt.show() 

:これらの答えの

enter image description here

関連する問題