Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python matplotlib-用其他标签值替换日志格式的xtick标签值_Python_Python 3.x_Matplotlib_Plot_Xticks - Fatal编程技术网

Python matplotlib-用其他标签值替换日志格式的xtick标签值

Python matplotlib-用其他标签值替换日志格式的xtick标签值,python,python-3.x,matplotlib,plot,xticks,Python,Python 3.x,Matplotlib,Plot,Xticks,我有以下代码片段,可以使用logscale对x轴进行绘图: # Number of values for loop num_prior_loop = 100 # Declare prior array prior_fish = np.zeros(num_prior_loop) prior_start = 0 prior_end = 3 prior_fish = np.logspace(prior_start,prior_end,num_prior_loop)**2 # Declare FoM f

我有以下代码片段,可以使用logscale对x轴进行绘图:

# Number of values for loop
num_prior_loop = 100
# Declare prior array
prior_fish = np.zeros(num_prior_loop)
prior_start = 0
prior_end = 3
prior_fish = np.logspace(prior_start,prior_end,num_prior_loop)**2
# Declare FoM final array
FoM_final = np.zeros(num_prior_loop)
...

# Plot
fig, ax = plt.subplots()    
ax.plot(prior_fish, FoM_final)
#ax.set_xticks([np.linspace(prior_start,prior_end)])
#ax.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
plt.xlabel(r'Prior on 5 ratios '+r'$\alpha = b_{sp}/b_{ph}$'+' in Fisher GCsp', fontsize=10)
plt.ylabel('FoM')
plt.title('FoM vs Prior - Prior on both GCsp and XC', fontsize=10)
plt.savefig('FoM_vs_Prior_alpha_on_both_GCsp_and_XC.pdf')
这将生成以下图形:

现在,我想做一个修改,也就是说,我希望xtick 10^0到10^6自动替换为1,10^-1,10^-2,…,10^-6

我说自动,因为2个边界(1和10^-6)的开始和结束可能会有所不同

更新1:我尝试了下面回答中
@max
建议的解决方案,但失败了:

这是我得到的:

如果我这样做:

for txt in axs.get_xticklabels():
    # get position
    x,y = txt.get_position()
    print('x,y =', x,y)
    # value/formatting
    val = txt.get_text()
    # create new label
    if x == 1 or x == 0:
        val_new = f'${round(x)}$'
    else:
        val_new = '$\\mathdefault{10^{' + f'{round(np.log10(1/x))}' + '}}$'
    # append
    xTickLabel.append( Text(x,y,val_new) )
# set labels
axs.set_xticklabels( xTickLabel )
# Plot and save
plt.plot(prior_fish, FoM_final)
plt.draw()
plt.savefig('FoM_vs_Prior_alpha_on_both_GCsp_and_XC.png')
我只得到零:

x,y = 0 0
x,y = 0 0
x,y = 0 0
x,y = 0 0
x,y = 0 0
x,y = 0 0
x,y = 0 0
x,y = 0 0
x,y = 0 0
x,y = 0 0
x,y = 0 0
x,y = 0 0
而打印
val
总是返回空值

下面是整个代码:

# Plot
fig, axs = plt.subplots()
axs.set_xscale('log')
plt.draw()

plt.xlabel(r'Prior on 5 ratios '+r'$\alpha = b_{sp}/b_{ph}$'+' in Fisher GCsp', fontsize=10)
plt.ylabel('FoM')
plt.title('FoM vs Prior - Prior on both GCsp and XC', fontsize=10)
# create new labels
xTickLabel = []
for txt in axs.get_xticklabels():
    # get position
    x,y = txt.get_position()
    # value/formatting
    val = txt.get_text()
    # create new label
    if x == 1 or x == 0:
        val_new = f'${round(x)}$'
    else:
        val_new = '$\\mathdefault{10^{' + f'{round(np.log10(1/x))}' + '}}$'
    # append
    xTickLabel.append( Text(x,y,val_new) )
# set labels
axs.set_xticklabels( xTickLabel )
# Plot and save
plt.plot(prior_fish, FoM_final)
plt.draw()
plt.savefig('FoM_vs_Prior.pdf')
臭虫在哪里?如何获取值“0”和“1”的不同值以避免出现这种情况

   if x == 1 or x == 0:
        val_new = f'${round(x)}$'

似乎您希望直接修改文本标签。看看下面的代码。我们获取记号的位置,反转
x
-值,并更改
matplotlib.text.text
-对象(或者更确切地说,我创建了新对象)。诀窍是位置保持不变;我们只需更改此位置/勾选标签上显示的文本:

从matplotlib导入pyplot作为plt
从matplotlib.text导入文本
将numpy作为np导入
图,axs=plt子批次(2,1)
#第一轴
axs[0]。设置xscale('log'))
axs[0]。设置刻度('log')
axs[0]。散射(2**np.arange(10),2**np.arange(10))
plt.draw()
#第二轴
axs[1]。设置xscale('log'))
axs[1]。设置刻度('log')
axs[1]。散射(2**np.arange(10),2**np.arange(10))
#创建新标签
xTickLabel=[]
用于axs[0]中的txt。获取\u xticklabels():
#获得位置
x、 y=txt.get_position()
#值/格式
val=txt.get_text()
#创建新标签
如果x==1或x==0:
val_new=f'${round(x)}$'
其他:
val_new='$\\mathdefault{10^{'+f'{round(np.log10(1/x))}'+'}$'
#附加
xTickLabel.append(文本(x,y,val_new))
#设置标签
axs[1]。设置xTickLabel(xTickLabel)
plt.draw()

您好,我得到了一个错误的数字,您可以在更新1中看到它,我不明白为什么打印(x,y)时只有零?你能看到错误在哪里吗?你的代码对我来说很好(我刚刚添加了
prerior\u fish,FoM\u final=2**np.arange(10),2**np.arange(10)
,以获得一个错误)。请注意,在调用
getxticklabels()
之前,需要绘制轴(但您这样做了)。我正在使用Python3.8和Matplotlob3.3.2Tanks为您提供解决方案,问题已经解决