gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:45:24 25 4
gpt4 key购买 nike

我是从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com