2017-05-03 34 views
0

私は複数のデータ系列を持つ棒グラフを持っていますが、x軸値を%.2fの有意な値に設定したいのですが、すでに最初のグラフのset_majorフォーマッタ値は0にリセットされますが、値は2番目のグラフのようになります。棒グラフのx軸値を%.2fに設定

どうすればこの問題を解決できますか?

This bar graph

このような私のコードを見て:

import matplotlib.pyplot as plt 
import pandas as pd 
import matplotlib.ticker as mtick 

# select the measurement location 
MATH = "import/data/place" 
SAVE = "save/location" 

fig, axes = plt.subplots(figsize=(12,15),nrows=2, ncols=1) # size of the plots and the placing 
fig.subplots_adjust(hspace=0.5) # set space between plots 

DATA = pd.read_csv(MATH,delimiter=',',usecols = [2,3,4,5,6,7,8,9,10,11,12],names = ['set_t','set_rh', 
        'type','math','ref','LUFFT','VPL','VPR','VVL','VVR','PRO'], parse_dates=True) 
# select the data 
temp = DATA.loc[(DATA['type']=='T')&(DATA['math']=='dif')] # dif temperature data 
rh = DATA.loc[((DATA['type']=='RH')&(DATA['math']=='dif'))] # dif relative humidity data 

# plot temperature 
fg = temp.plot.bar(x='set_t',y = ['LUFFT','VPL','VPR','VVL','VVR','PRO'], 
        color = ['b','firebrick','orange','forestgreen','darkturquoise','indigo'], 
        ax=axes[0]) 

fg.grid(True) 
fg.set_ylabel('$ΔT$(°C)',fontsize = 12) 
fg.set_xlabel('ref $T$ (°C)',fontsize = 12) 
fg.set_title('Difference in T from reference at constant relative humidity 50%',fontsize = 15) 
fg.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.2f')) 
fg.xaxis.set_major_formatter(mtick.FormatStrFormatter('%.2f')) 

# plot relative humidity 
df = rh.plot.bar(x='set_t',y = ['LUFFT','VPL','VPR','VVL','VVR','PRO'], 
        color = ['b','firebrick','orange','forestgreen','darkturquoise','indigo'], 
        ax=axes[1]) 
df.grid(True) 
df.set_ylabel('$ΔU$(%)',fontsize = 12) 
df.set_xlabel('ref $T$ (°C)',fontsize = 12) 
df.set_title('Difference in U from reference at constant relative humidity 50%',fontsize = 15) 

plt.tight_layout() 
plt.savefig(SAVE + "_example.jpg") 
plt.show() 

私のデータのサンプル:

07:40:00,07:50:00,39.85716354999982,51.00504745588235、 T、dif ,, 0.14283645000018197、-0.07502069285698099、-0.15716354999978677,0.00202、-0.07111703837193772、-0.0620802166664447,

07:40:00,07:50:00,39.85716354999982,51.00504745588235、RH、DIF ,, - 0.40504745588239643,3.994952544117652,2.994952544117652,4.994952544117652、6.994952544117652、

08:40:00,08:50:00、 34.861160704969016,51.1297401832298、T、DIF ,, 0.22883929503095857,0.2509082605481865、-0.2575243413326831,0.24864321659958222,0.14092262836431502、-0.04441070496899613、

08:40:50:00,08 00,34.861160704969016,51.1297401832298、RH、DIF ,, - 0.32974018322978793 、3.8702598167702007,2.8702598167702007,4.870259816770201は、6.870259816770201、

+0

質問を編集してCSVの値の小さなサンプルを含めると、問題を再現しやすくなります。 –

+0

質問に追加しました – Sanne

答えて

2

これはトンのようbarplotグループ化しているという事実によるものですx軸が実際の「距離」を失い、ティック位置に関連付けられた値が位置そのものになります。それはちょっと暗いですが、fg.get_xlim()で、値が元のデータとの「接触」を失っており、単純に整数を増やしていることが分かります。基本的に

def check_pos(val, pos):  
    print(val, pos)  
    return '%.2f' % val 

これは何のフォーマッタがあなたのケースのために仕事に行くされていないことを示しています。あなたは/デバッグ「の値」とあなたはこのような機能をFuncFormatterを提供する場合のポジション "matplotlibのは、使用を探索することができます。

幸いなことに、ticklabelsは正しく(テキストとして)設定されているため、これらを解析して浮動小数点数にすることができます。

完全にあなたのフォーマッタを外し、でxticklabelsを設定します。

fg.set_xticklabels(['%.2f' % float(x.get_text()) for x in fg.get_xticklabels()]) 

注matplotlibの自体がバープロットとの組み合わせで正しいtickvaluesを保存する完全に可能ですが、「グループ化をしなければならないであろうと'など、あなた自身もそうではありません。

+0

ありがとうございます! – Sanne

関連する問題