我想知道是否有一种方法可以为轴上的刻度标签设置不同的格式。在我的代码下面,我尝试了两个选项,第一个工作但是y标签对于我的图形尺寸来说太大了(我不能改变),第二个选项我当时试图改变一个刻度标签格式,但它没有显示任何内容,所以我的问题是要知道是否有解决办法。感谢
PS:第一次使用matplotlib进行图表
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator
from matplotlib.ticker import LogLocator
import matplotlib.ticker as mtick
fig, ax = plt.subplots()
#Define only minor ticks
minorLocator = LogLocator(base=10,subs='auto')
#Set the scale as a logarithmic one
ax.set_xscale('log')
ax.set_yscale('log')
ax.set_ylim(0.00001,1000)
#Set the minor ticks
ax.xaxis.set_minor_locator(minorLocator)
ax.yaxis.set_minor_locator(minorLocator)
vals = ax.get_xticks()
ax.set_xticklabels(['{:3.2f}'.format(x) for x in vals])
'''This line below if uncommented works, but the format is not
correct and only 7 characters are displayedMy y values will be
1000.00000, 100.00000,10.0000, 1.00000, 0.10000,
etc.. '''
#ax.yaxis.set_major_formatter(mtick.PercentFormatter(decimals=5))
''' With the lines of code below which does not work, I am trying to have
the axis as 1000.00, 100.00, 10.00, 1.00, 0.1, 0.01, 0.001, 0.0001, etc..
Nothing however is displayed'''
vals = ax.get_yticks()
for x in vals:
if x > 0.001:
ax.set_yticklabels(['{:7.2f}%'.format(x*1)])
else:
ax.set_yticklabels(['{:7.5f}%'.format(x*1)])
答案 0 :(得分:1)
答案 1 :(得分:1)
创建一个以正确格式返回字符串的函数,然后使用matplotlib.ticker.FuncFormatter将函数指定为y轴格式化程序。假设您希望y轴标签看起来像这样:http://127.0.0.1:8888/user/dist/main.css
并从this进行调整:
1000.00, 100.00, 10.00, 1.00, 0.1, 0.01, 0.001, 0.0001