2016-12-07 6 views
-1

私はSpyder(python 2.7)を使っていて、matplotlibを使ってグラフをプロットしています。私はコードの塊(下記参照)を持っているとき、私はコードを手動で強調表示して実行するとすべてがうまく見えます。しかし、完全なスクリプトの実行ボタンを押すか、ループ内でこのコードを実行すると、y-tickラベルが各プロットから消えます。私の人生は問題を見つけることができません。私は完全なPythonスクリプト(matplotlib)を実行するとY字目のラベルが消えます

data2plot = [120.0, 56.0, 26.0, 11.0, 6.0, 6.0, 5.0, 5.0, 3.0, 3.0, 2.0, 2.0, 2.0] 
bar_locs = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0] 
x_labs = ['','','','','','','','','','','','',''] 
fig, ax = plt.subplots() 
#plot_help.bigfig()  
rects = ax.bar(bar_locs, data2plot, 0.75, color='steelblue', align='center')     
ax.set_xticks(bar_locs) 
ax.set_xticklabels(x_labs, fontweight='bold', fontsize=13, rotation=-45, ha='left')  
ax.set_xlim(0, len(x_labs)+0.75) 
#ax.set_ylim(0, y_max) 
ax.set_ylabel('Pageviews', fontweight='bold', fontsize=15) 
ax.set_yticklabels(ax.get_yticklabels(), fontweight='bold', fontsize=13) 
ax.set_title('Title', fontweight='bold', fontsize=16)  

plt.gcf().subplots_adjust(bottom=0.45) 
#plot_help.simpleaxis(ax) 
+0

有効なコードは投稿しないでください。代わりに、エラーを返すコードを投稿してください。 – ImportanceOfBeingErnest

+0

このスクリプトで "run"を押した場合、このコードはグラフにy-tickラベルが付かないという意味で "エラー"を出します。 – pavlov

+0

変数がないので、誰もそれを確認することはできません。回答を得ることに興味がある場合は、[MCVE]を提供する必要があります。 – ImportanceOfBeingErnest

答えて

0

問題はこの行にありますax.set_yticklabels(ax.get_yticklabels(), ...)
ax.get_yticklabels()と呼んだ瞬間に、ティックラベルはまだ設定されていないので、ax.get_yticklabels()は空の文字列のリストを返します。これらの空の文字列は新しいyticklabelsとして設定されます。 手先fig.canvas.draw()を電話すると、これが起こらないようにすることができます。これは図を描画し、セッターに与えられたときにリストが空文字列にならないようにティックラベルを初期化します。

ここに完全なコードがあります。あなたの意図は、大胆かつ大きな図にすべてのテキスト、ラベルとタイトルを取得する場合

import matplotlib.pyplot as plt 

data2plot = [120.0, 56.0, 26.0, 11.0, 6.0, 6.0, 5.0, 5.0, 3.0, 3.0, 2.0, 2.0, 2.0] 
bar_locs = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0] 
x_labs = ['','','','','','','','','','','','',''] 

fig, ax = plt.subplots() 
rects = ax.bar(bar_locs, data2plot, 0.75, color='steelblue', align='center') 

# x-axis    
ax.set_xticks(bar_locs) 
ax.set_xticklabels(x_labs, fontweight='bold', fontsize=13, rotation=-45, ha='left')  
ax.set_xlim(0, len(x_labs)+0.75) 

#y-axis 
ax.set_ylabel('Pageviews', fontweight='bold', fontsize=15) 
fig.canvas.draw() 
ax.set_yticklabels(ax.get_yticklabels(), fontweight='bold', fontsize=13) 


ax.set_title('Title', fontweight='bold', fontsize=16)  
plt.gcf().subplots_adjust(bottom=0.45) 
plt.show() 


あるいは、あなたは次のように matplotlib rcParamsを使用して検討するかもしれません。

plt.rc('font', size=13, weight='bold') 
plt.rc('figure', titlesize="x-large", titleweight ='bold') 
plt.rc("axes", labelsize = "large", labelweight = "bold", titlesize="x-large", titleweight="bold") 

data2plot = [120.0, 56.0, 26.0, 11.0, 6.0, 6.0, 5.0, 5.0, 3.0, 3.0, 2.0, 2.0, 2.0] 
bar_locs = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0] 
x_labs = ['','','','','','','','','','','','',''] 

fig, ax = plt.subplots() 
rects = ax.bar(bar_locs, data2plot, 0.75, color='steelblue', align='center') 

# x-axis    
ax.set_xticks(bar_locs) 
ax.set_xticklabels(x_labs, rotation=-45, ha='left')  
ax.set_xlim(0, len(x_labs)+0.75) 

#y-axis 
ax.set_ylabel('Pageviews') 

ax.set_title('Title')  
plt.gcf().subplots_adjust(bottom=0.45) 
plt.show() 
関連する問題