2017-01-22 46 views
1

我是新來自C和Matlab的Python。我正在創建一個腳本,爲洪水頻率分析產生一個對數概率(log yaxis-probability xaxis)圖。我使用了以下解決方案#1爲x軸的概率縮放:yaxis標籤沒有顯示與自定義xaxis概率比例python

Creating Probability/Frequency Axis Grid (Irregularly Spaced) with Matplotlib

該解決方案的偉大工程爲x軸。但是,當我將yaxis縮放爲log10時,yaxis標籤消失。這裏是用來創建劇情的代碼;在「概率」的呼叫是指概率軸使用上述#1解決方案縮放:

# Step 1: load the needed pacakages 
import numpy as np 
import matplotlib.pyplot as plt 
from numpy import ma 
from matplotlib import scale as mscale 
from matplotlib import transforms as mtransforms 
from scipy.optimize import curve_fit 

# Step 2: Load up some files and specify variables 
# I have not included this part of the code b/c it works fine 

# Step 3: Execute the xaxis proability scaling code referenced above 

# Step 4: Create a figure 
fig = plt.figure(1) 
# Call the firts subplot 
ax = fig.add_subplot(2,1,1) 
# Create the first subplot 
scatter, = ax.plot(NE,Floods,'mo') 
# Grab the axes 
ax = plt.gca()  
# Set the axis lables 
ax.set_ylabel('Discharge in CMS') 
ax.set_xlabel('Non-exceedance Probability') 

#Adjust the yaxis format 
ax.set_yscale('log')  
ax.set_ylim((0.01, 1000)) 
plt.tick_params(axis='y', which='major')  
ax.yaxis.set_major_locator(FixedLocator([0.1,1,10,100,1000])) 

# Specify the xaxis tick labels  
points = np.array([0.1,1,2,5,10,20,30,40,50,60,70,80,90,95,99,99.9])  

# Set the x-axis scale, labels and format 
ax.set_xscale('probability', points = points, vmin = .01) 
xlabels=points 
ha = ['right', 'center', 'left'] 
ax.set_xticklabels(xlabels, rotation=-90, ha=ha[1]) 

# Specify no grid  
plt.grid(False) 
# Show the plot 
plt.show() 

這裏是由此得出的數字是什麼樣的 - 注意缺乏y軸的刻度或刻度標籤:
Here is what the resulting figure looks like - note the lack of yaxis ticks or tick labels:

任何可以提供的幫助將不勝感激。謝謝。

+0

請提供一個運行腳本:'NameError:name'NE'is not defined''。 –

+0

在提問之前,請[參觀](http://stackoverflow.com/tour)。在那裏你會發現創建一個[MCVE]很重要。雖然你遺漏的部分對你來說工作正常,但它會阻止其他人驗證你的例子。對於有經驗的用戶來說,現在需要10分鐘來生成一些數據,以運行代碼,而提供解決方案通常需要1分鐘以內。這裏的問題是FixedLocator,請參閱[這個問題](http://stackoverflow.com/questions/14530113/set-ticks-with-logarithmic-scale)上的一個可能的選擇。 – ImportanceOfBeingErnest

+0

我想編輯我的問題發佈整個代碼,但我不能編輯問題....我是新來的Stackoverflow。 – SurfProc

回答

1

感謝ImportanceOfBeerErnest。我發現,在所建議的鏈接一個可行的解決方案:

set ticks with logarithmic scale

的代碼修改是更換FixedLocator呼叫,以及它上面的呼叫,用於設置yticks一個呼叫,並且該格式化。下面是原始和修改後的代碼

原始代碼:

#Adjust the yaxis format 
ax.set_yscale('log')  
ax.set_ylim((0.01, 1000)) 
plt.tick_params(axis='y', which='major')  
ax.yaxis.set_major_locator(FixedLocator([0.1,1,10,100,1000])) 

這裏是修改後的代碼:

#Adjust the yaxis format 
ax.set_yscale('log')  
ax.set_ylim((0.01, 1000)) 
ax.set_xticklabels(["0.01", "0.1", "1", "10", "100", "1000"]) 
ax.get_xaxis().set_major_formatter(plt.ticker.ScalarFormatter()) 

下面是由此得出的數字與修改後的代碼:

plot with correct yaxis

該問題已回答。再次感謝。