Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use matplotlib set_yscale

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?

like image 806
ShanZhengYang Avatar asked Jul 02 '15 20:07

ShanZhengYang


1 Answers

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')
>>> 
like image 93
Srivatsan Avatar answered Oct 07 '22 23:10

Srivatsan