我正在使用Matplotlib绘制一个函数,并且我想将y轴设置为log-scale。
但是,我使用set_yscale()
时总是遇到错误。在设置了x值和y值的值之后,我写道
import matplotlib as plt
plot1 = plt.figure()
plt.plot(x, y)
plt.set_xscale("log")
这将导致以下错误:
AttributeError: 'module' object has no attribute 'set_xscale'
所以,我试着
plot1 = plt.figure()
plt.plot(x, y)
plt.set_xscale("log")
我得到了同样的错误。
如何调用此函数?
发布于 2015-07-03 04:25:30
直接使用matplotlib.pyplot
调用图形时,只需使用plt.xscale('log')
或plt.yscale('log')
调用它,而不是使用plt.set_xscale('log')
或plt.set_yscale('log')
仅当使用的轴实例如下所示时:
fig = plt.figure()
ax = fig.add_subplot(111)
您可以使用以下命令调用它:
ax.set_xscale('log')
示例:
>>> import matplotlib.pyplot as plt
>>> plt.set_xscale('log')
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
plt.set_xscale('log')
AttributeError: 'module' object has no attribute 'set_xscale'
>>> plt.xscale('log') # THIS WORKS
>>>
然而,
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111)
>>> ax.set_xscale('log')
>>>
https://stackoverflow.com/questions/31193976
复制相似问题