2016-04-15 9 views
0

イントロ

私はPython、matplotlib、pandasを初めて使っています。私は次のことを考え出すために多くの時間を費やしました。そして私は立ち往生している。パンダのデータフレーム描画:yscaleログとxyラベルと伝説の問題

質問:

私はパンダを使用してプロットしようとしています。私は3つのY軸を持ち、そのうちの1つは対数スケールです。 私のセカンダリ軸ax2のコードで、ログ機能()とラベル機能()が機能しない理由を理解できません。他のすべての場所で動作します。

すべての凡例が分離されています()。これを手動で行うよりも簡単な方法がありますか?

セカンダリ軸の部分をプロットすると、別に問題なく出力されます。私は3番目の軸を削除するプロットを実行し、まだ問題が続く。この方法で一緒に働くことが提案されたソリューションが必要なときに、私はここにすべての軸を含むコードを入れました。

Here()のみを解決する方法がありますが、特にデータフレームベースのプロットを探しています。同じサイトには他にも手作業のテクニックがありますが、私は使いたくありません!

enter image description here

コードと説明

# Importing the basic libraries 
import matplotlib.pyplot as plt 
from pandas import DataFrame 

# test3 = Dataframe with 5 columns 
test3 = df.ix[:,['tau','E_tilde','Max_error_red','time_snnls','z_t_gandb']] 

# Setting up plot with 3 'y' axis 
fig, ax = plt.subplots() 
ax2, ax3 = ax.twinx(), ax.twinx() 
rspine = ax3.spines['right'] 
rspine.set_position(('axes', 1.25)) 
ax3.set_frame_on(True) 
ax3.patch.set_visible(False) 
fig.subplots_adjust(right=0.75) 

# Setting the color and labels 
ax.set_xlabel('tau(nounit)') 
ax.set_ylabel('Time(s)', color = 'b') 
ax2.set_ylabel('Max_error_red', color = 'r') 
ax3.set_ylabel('E_tilde', color = 'g') 

# Setting the logscaling 
ax.set_xscale('log') # Works 
ax2.set_yscale('log')# Doesnt work 

# Plotting the dataframe 
test3.plot(x = 'tau', y = 'time_snnls', ax=ax, style='b-') 
test3.plot(x = 'tau', y = 'Max_error_red', ax=ax2, style='r-', secondary_y=True) 
test3.plot(x = 'tau', y = 'z_t_gandb', ax=ax, style='b-.') 
test3.plot(x = 'tau', y = 'E_tilde', ax=ax3, style='g-') 

答えて

1

問題がsecondary_y=Trueオプションです。それを削除し、それは正常に動作します。私はあなたの双軸を既に設定していて、secondary_y=Trueがそれを妨害しているという問題があると思います。

凡例については、test3.plotコマンドのそれぞれにlegend=Falseを設定し、ax.get_legend_handles_labels()を使用してプロットした後、凡例のハンドルとラベルを軸から収集します。それで、それらをすべて一つの凡例にプロットすることができます。

最後に、軸ラベルが正しく設定されていることを確認するために、あなたはパンダDataFrameプロットする方法は、あなたが設定しようとしたものは何でも上書きされますよう、あなたが、あなたのデータをプロットしている後にそれらを設定する必要があります。後でこれを行うことで、それがあなたのラベルであることを確認します。相続人

(ダミーデータ付き)作業スクリプト:

import matplotlib.pyplot as plt 
from pandas import DataFrame 
import numpy as np 

# Fake up some data 
test3 = DataFrame({ 
    'tau':np.logspace(-3,0,100), 
    'E_tilde':np.linspace(100,0,100), 
    'Max_error_red':np.logspace(-2,1,100), 
    'time_snnls':np.linspace(5,0,100), 
    'z_t_gandb':np.linspace(16,15,100) 
    }) 

# Setting up plot with 3 'y' axis 
fig, ax = plt.subplots() 
ax2, ax3 = ax.twinx(), ax.twinx() 
rspine = ax3.spines['right'] 
rspine.set_position(('axes', 1.25)) 
ax3.set_frame_on(True) 
ax3.patch.set_visible(False) 
fig.subplots_adjust(right=0.75) 

# Setting the logscaling 
ax.set_xscale('log') # Works 
ax2.set_yscale('log')# Doesnt work 

# Plotting the dataframe 
test3.plot(x = 'tau', y = 'time_snnls', ax=ax, style='b-',legend=False) 
test3.plot(x = 'tau', y = 'Max_error_red', ax=ax2, style='r-',legend=False) 
test3.plot(x = 'tau', y = 'z_t_gandb', ax=ax, style='b-.',legend=False) 
test3.plot(x = 'tau', y = 'E_tilde', ax=ax3, style='g-',legend=False) 

# Setting the color and labels 
ax.set_xlabel('tau(nounit)') 
ax.set_ylabel('Time(s)', color = 'b') 
ax2.set_ylabel('Max_error_red', color = 'r') 
ax3.set_ylabel('E_tilde', color = 'g') 

# Gather all the legend handles and labels to plot in one legend 
l1 = ax.get_legend_handles_labels() 
l2 = ax2.get_legend_handles_labels() 
l3 = ax3.get_legend_handles_labels() 

handles = l1[0]+l2[0]+l3[0] 
labels = l1[1]+l2[1]+l3[1] 

ax.legend(handles,labels,loc=5) 

plt.show() 

enter image description here

+0

うわーは、yourquickと作業応答、は、xlabelはまだ変更されません。ただ、もう一つのことをお願いいたします。それは 'タウ(nounit)'とは対照的に 'タウ'です。あなたは何が起こっているのか知っていますか?私たちは再び集める必要がありますか?そのために使用される方法は何ですか?ありがとうございました。 –

+1

'ax.set_xlabel'行を' test3.plot'行の後ろに移動してみてください – tom

+1

@ThejKiran: 'set_ {x、y} label'行を' plot'行の後ろに動かすことが確かですこの問題を修正してください。私の編集を見る – tom

関連する問題