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?
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')
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With