Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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在y轴上渲染错误的记号_Python_Python 3.x_Matplotlib - Fatal编程技术网

Python Matplotlib在y轴上渲染错误的记号

Python Matplotlib在y轴上渲染错误的记号,python,python-3.x,matplotlib,Python,Python 3.x,Matplotlib,我试图使用matplotlibPython包在y轴上渲染指定的记号。由于某种原因,该库渲染的记号比我为它们渲染和使用科学符号所给的记号要多。对我来说,这似乎是图书馆里的一个bug,我能够在更多的设备上重现这个问题。有解决办法吗 再现问题的源代码: import matplotlib.pyplot as plt import matplotlib.ticker as mticker plt.figure(figsize=(6,4)) plt.xlim(0, 1000) plt.xlabel('x'

我试图使用
matplotlib
Python包在y轴上渲染指定的记号。由于某种原因,该库渲染的记号比我为它们渲染和使用科学符号所给的记号要多。对我来说,这似乎是图书馆里的一个bug,我能够在更多的设备上重现这个问题。有解决办法吗

再现问题的源代码:

import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
plt.figure(figsize=(6,4))
plt.xlim(0, 1000)
plt.xlabel('x')
plt.yscale('log')
plt.ylim(20, 200)
plt.yticks([20, 28, 39, 54, 75, 110, 150, 200])
plt.gca().get_yaxis().set_major_formatter(mticker.ScalarFormatter(useOffset=False))
plt.gca().get_yaxis().set_tick_params(which='minor', width=0)
plt.ylabel('y')
plt.savefig('test.png')
plt.close()
渲染的图形:

编辑
plt.yscale('log')
之后调用
plt.gca().get_yaxis().clear()
似乎可以解决问题。

注释
plt.yscale('log')
似乎可以从y轴删除科学符号,同时保留所需的刻度值

如果您仍然需要它,将
plt.yscale('log')
放在
plt.ylim()
plt.yticks()
之后似乎可以:

import matplotlib.pyplot as plt
import matplotlib.ticker as mticker

plt.figure(figsize=(6,4))
plt.xlim(0, 1000)
plt.xlabel('x')
plt.ylim(20, 200)
plt.yticks([20, 28, 39, 54, 75, 110, 150, 200])
plt.yscale('log')
plt.gca().get_yaxis().set_major_formatter(mticker.ScalarFormatter(useOffset=False))
plt.gca().get_yaxis().set_tick_params(which='minor', width=0)
plt.ylabel('y')
plt.savefig('test.png')
plt.close()

为了将100替换为10^2,我对第一行
plt.gca()
进行了注释:


谢谢,你的建议给了我正确的方向。在
plt.yscale('x')
之后调用
plt.gca().get_yaxis().clear()
似乎可以解决问题。它们都是小刻度。使用
plt.minorticks\u off()
您是否尝试关闭小刻度
set_minor\u formatter(mticker.NullFormatter())
在更改格式化程序之前,它们不是小刻度,而是
plt.yscale('log')
调用后的原始轴。解决了这个问题。