Matplotlib can't find documented function set_cmap - python

I have the following code:
import matplotlib.pyplot as plt
plt.cm.set_cmap("Blues")
This gives me an error:
Traceback (most recent call last):
File ".\lorenz_explorer.py", line 12, in <module>
plt.cm.set_cmap("Blues")
AttributeError: module 'matplotlib.cm' has no attribute 'set_cmap'
My matplotlib version is 3.3.1, and the function certainly exists in the documentation for 3.3.1: Link
Then am I doing something wrong or is this a bug? Do I need to import matplotlib.cm separately or something along those lines?

As the documentation link you provide shows, the name of the function is matplotlib.pyplot.set_cmap, not matplotlib.pyplot.cm.set_cmap. So you can call it with plt.set_cmap("Blues").
In other words, the function is not part of the cm library, which is somewhat counter-intuitive.

Related

i need to resolve error highlighted in my question

NameError Traceback (most recent call last)
C:\Users\HTCOMP~1\AppData\Local\Temp/ipykernel_6676/988855770.py in <module>
----> 1 sns.distplot(data_no_mv['body'])
NameError: name 'sns' is not defined
sns usually represents the seaborn library, so you might need to add import seaborn as sns to the beginning of your code. You might already have done this and something else might be wrong, but it is hard to tell without the code.

Is there a substitute for matplotlib.pyplot.colors?

I am trying the function on this doc
import matplotlib.pyplot as plt
plt.colors()
got this error
--------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-1-e2c1adbcc48e> in <module>
1 import matplotlib.pyplot as plt
----> 2 plt.colors()
AttributeError: module 'matplotlib.pyplot' has no attribute 'colors'
the doc says
The colors function was deprecated in version 2.1.
without providing a substitute.
is there a substitute for matplotlib.pyplot.colors?
To be more precise here: No, there is no substitude for matplotlib.pyplot.colors because its only purpose was to allow users to get help via help(plt.colors). It was considered more harmful to confuse users by the presence of this function, which doesn't do anything, than it to be useful to let them get help on colors via pyplot. If you want to get help on colors now, you may still type
help(matplotlib.colors)
though that is a bit more lengthy.

'function' object has no attribute 'plot'

I was following this tutorial https://www.kaggle.com/residentmario/univariate-plotting-with-pandas
and trying to do the exercise mentioned with the pokemon database but whenever I try to implement the code below I get the error mentioned below and don't understand what to do. I am using matplotlib.use('agg') because I was getting an error related to Tkinter. I am using pycharm, python 3.6 and I am on ubuntu 18.04
Here is my code:
import pandas as pd
import matplotlib
matplotlib.use('agg')
from matplotlib.pyplot import plot
df=pd.read_csv("/home/mv/PycharmProjects/visualization/pokemon.csv")
df['type1'].value_counts.plot(kind='bar')
error
Traceback (most recent call last):
File "/home/mv/PycharmProjects/visualization/univariate plotting.py",
line 9, in <module>
df['type1'].value_counts.plot(kind='bar')
AttributeError: 'function' object has no attribute 'plot'
The error states that df['type1'].value_counts is a function.
To plot the result of the function change:
df['type1'].value_counts.plot(kind='bar')
into
df['type1'].value_counts().plot(kind='bar')

Scipy - Error while using spherical Bessel functions

I'm trying to draw plots in Python with Scipy module. According to http://docs.scipy.org/doc/scipy/reference/special.html I wrote code with scipy.special.spherical_jn(n,x,0):
import matplotlib.pyplot as plt
import numpy as np
import scipy.special as sp
from matplotlib import rcParams
rcParams.update({'figure.autolayout': True})
def odrazTE(a,o,d):
temp1 = sp.spherical_jn[1,a,0]
temp2 = 1
return abs(temp1/temp2)**2
t = np.arange(0.001, 2, 0.001)
plt.plot(t,odrazTE(t,t,1),label='TE1')
plt.show()
While I'm compiling the program, all I get is this error:
Traceback (most recent call last):
File "standing-sphere.py", line 33, in <module>
plt.plot(t,odrazTE(t,t,1),label='TE1')
File "standing-sphere.py", line 15, in odrazTE
temp1 = sp.spherical_jn[1,a,0]
AttributeError: 'module' object has no attribute 'spherical_jn'
There is way how to do it with regular Bessel function and relationship between Bessel and spherical Bessel function, but I don't like this solution because of derivative of sph.bess. function that I need too.
Is there any chance I have set something wrongly and it can be fixed to scipy.special.spherical_jn work?
scipy.special.spherical_jn was added in scipy version 0.18.0, which was released on July 25, 2016. My guess is you are using an older version of scipy. To check, run
import scipy
print(scipy.__version__)

Renderer problems using Matplotlib from within a script

I've narrowed down to this call:
fig.canvas.tostring_argb() #fig=matplotlib.pyplot.figure()
this function raises an AttributeError when I run the code as a python script.
AttributeError: 'FigureCanvasGTKAgg' object has no attribute 'renderer'
However, this code works properly if run in the ipython --pylab command line.
As far as I can tell from the documentation, the Agg renderer should work OK.
The context is that I'm trying to make a movie from figures, without saving the frames
to disk; as per this question. I'm using the approach that streams the pixel arrays
to ffmpeg (running as a separate process) to do this, I need the argb array of values from the frame.
Is there some configuration setting I can make to get matplotlib to work correctly from within a script?
Edit
Tried use('Agg') as per a comment; still fails; this is a minimal working example.
[dave#dave tools]$ python -c "import matplotlib; matplotlib.use('Agg'); import matplotlib.pyplot; fig=matplotlib.pyplot.figure(); fig.canvas.tostring_argb()"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 416, in tostring_argb
return self.renderer.tostring_argb()
AttributeError: FigureCanvasAgg instance has no attribute 'renderer'
I suspect that you have missed out the call to:
fig.canvas.draw()
before
fig.canvas.tostring_argb()
as
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot
fig=matplotlib.pyplot.figure()
fig.canvas.tostring_argb()
fails for me, but
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot
fig=matplotlib.pyplot.figure()
fig.canvas.draw()
fig.canvas.tostring_argb()
works.
I ended up installing and using the WXAgg backend; the Agg,and default GTKAgg, didn't work for me.

Categories