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

标签 python matplotlib

我是从 C 和 Matlab 开始接触 Python 的新手。我正在创建一个脚本,该脚本可生成用于洪水频率分析的对数概率(对数 y 轴-概率 x 轴)图。我正在使用以下 Stackoverflow 解决方案进行 xaxis 概率缩放:

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

这个解决方案非常适合 xaxis。但是,当我将 y 轴缩放为 log10 时,y 轴标签消失。这是用于创建绘图的代码; “概率”调用是指使用上述 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()

结果图如下 - 请注意缺少 y 轴刻度或刻度标签:
Here is what the resulting figure looks like - note the lack of yaxis ticks or tick labels:

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

最佳答案

感谢欧内斯特的存在的重要性。我在建议的链接中找到了一个可行的解决方案:

set ticks with logarithmic scale

代码修改是用设置 yticks 和 Formatter 的调用来替换 FixLocator 调用及其上面的调用。这是原始代码和修改后的代码

原始代码:

#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

问题已得到解答。再次感谢。

关于python - yaxis标签不显示自定义xaxis概率尺度python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41797385/

相关文章:

python - 导入错误 : No module names 'matplotlib' Python 3. 3

python - Matplotlib 正在打印线图两次/多次

python - 无法使用 ffmpeg 保存 Matplotlib 动画

python - RFID读写器接收到的不稳定数据如何处理?

python - (如何)我可以在命令行上使用类似于 pdb 的 pydevd 吗?

python - 将表表示为矩阵

python - 在 matplotlib 中使用适当大小的轴创建绘图

python - 使用 matplotlib 的多个并排直方图?

python - 如何在OpenCV(Python)中合并边界框

python : getting into imbedded document