yaxis标签不显示自定义xaxis概率标度python

2024-04-26 02:57:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚从C和Matlab接触Python。我正在创建一个脚本,它生成对数概率(logyaxis-probability xaxis)图,用于洪水频率分析。我正在使用以下Stackoverflow解决方案进行xaxis概率缩放:

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

这个解决方案非常适合xaxis。但是,当我将yaxis缩放为log10时,yaxis标签将消失。以下是用于创建绘图的代码;“概率”调用指使用上述Stackoverflow解决方案的概率轴缩放:

# 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()

结果图如下所示-注意缺少yaxis记号或记号标签:
Here is what the resulting figure looks like - note the lack of yaxis ticks or tick labels:

如能提供任何帮助,我们将不胜感激。谢谢。在


Tags: thefromimportmatplotlibasstepplt解决方案
1条回答
网友
1楼 · 发布于 2024-04-26 02:57:51

多亏了重要的Beingernest。我在建议的链接上找到了一个可行的解决方案:

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]))

以下是修改后的代码:

^{pr2}$

下面是修改代码后的结果图:

plot with correct yaxis

这个问题已经回答了。再次感谢。在

相关问题 更多 >