How to use matplotlib set_yscale

送分小仙女□ 提交于 2021-02-07 15:33:46

问题


I am plotting a function with Matplotlib, and I would like to set the y-axis to log-scale.

However, I keep getting an error using set_yscale(). After setting values for my x values and y values, I write

import matplotlib as plt

plot1 = plt.figure()
plt.plot(x, y)
plt.set_xscale("log")

This results in the error:

AttributeError: 'module' object has no attribute 'set_xscale'

So, I try

plot1 = plt.figure()
plt.plot(x, y)
plt.set_xscale("log")

I get the same error.

How do you call this function?


回答1:


When you are calling your figure using matplotlib.pyplot directly you just need to call it using plt.xscale('log') or plt.yscale('log') instead of plt.set_xscale('log') or plt.set_yscale('log')

Only when you are using an axes instance like:

fig = plt.figure()
ax = fig.add_subplot(111)

you call it using

ax.set_xscale('log')

Example:

>>> 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
>>> 

However

>>> fig = plt.figure()
>>> ax = fig.add_subplot(111)
>>> ax.set_xscale('log')
>>> 


来源:https://stackoverflow.com/questions/31193976/how-to-use-matplotlib-set-yscale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!