我是C和Matlab的Python新手。我正在创建一个脚本,该脚本生成用于洪水频率分析的对数概率(对数y轴-概率x轴)图。我将以下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刻度或刻度标签:
可以提供的任何帮助将不胜感激。谢谢。
最佳答案
感谢ImportanceOfBeingErnest。我在建议的链接中找到了可行的解决方案:
set ticks with logarithmic scale
代码修改是将FixLocate调用及其上方的调用替换为设置yticks和Formatter的调用。这是原始代码和修改后的代码
原始代码:
#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())
这是带有修改后的代码的结果图:
问题已经回答。再次感谢。
关于python - yaxis标签未与自定义xaxis概率标度一起显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41797385/