File "C:/Users/Vrushab PC/Downloads/Dissertation/untitled0.py", line 125, in
matplotlib.rcParams['figure.figsize'] = (50, 50)
NameError: name 'matplotlib' is not defined
I have imported matplotlib.pyplot but still getting this? what could be the reason?
Try this:
import matplotlib as mpl
mpl.rcParams['figure.figsize'] = (50, 50)
From matplotlib docs.
You are importing pyplot framework from matplotlib(a part of matplotlib), you don't have access to the full matplotlib library until you do the above import as mpl. Now, you can use mpl alias to access rcParams.
Related
I'm using python 3.6 in Jupyter lab on a linux mint machine to run this snippet of code
import matplotlib.pyplot as plt
fig = plt.figure()
man = plt.get_current_fig_manager()
man.window.setWindowTitle("New Title")
..it returns the following error message:
AttributeError: 'FigureManagerBase' object has no attribute 'window'
I have checked the GUI backends. All are available and all of them return this error even if I force the backend using ...
import matplotlib
matplotlib.use(<gui>,warn=False, force=True)
...before importing pyplot. The code has been working OK in Spyder but I've had to move to Jupyter. The matplotlib docs say that FigureManagerBase attributes include 'window'. I'm stumped
To change the window title, use the code below:
import matplotlib.pyplot as plt
fig = plt.figure()
man = plt.get_current_fig_manager()
man.canvas.set_window_title("New Title")
Using python 3.8, matplotlib 3.5.3 the following works for me:
import matplotlib.pyplot as plt
fig = plt.figure()
man = plt.get_current_fig_manager()
man.set_window_title("New Title")
I am having an issue where I cannot call the 'pyplot' element of 'matplotlib'. From the code below you can see I've had to add a "TkAgg" for the mattplotlib element to work, which is a common issue.
import matplotlib
matplotlib.use("TkAgg")
However, now I cannot add the '.pyplot' to the import. I've tried the following:
import matplotlib.pyplot as plt
plt.use("TkAgg")
But this gives me the error:
AttributeError: module 'matplotlib.pyplot' has no attribute 'use'
How can I get around this as my code requires pyplot to function, but I cannot work out how to import it while still having to use ".use("TkAgg").
I am running Python 3.6.2 and I am using Tkinter to develop my program
Those are two entirely different things. You import matplotlib to be able to set the backend. Then you need to still import pyplot to be able to use it afterwards.
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
# ... rest of code
If you use the use() function, this must be done before importing matplotlib.pyplot. Calling use() after pyplot has been imported will have no effect.
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
check with :
matplotlib.get_backend()
I am using python 3.6 and a learner. Below is a simple code of a sin wave.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10 , 10, 100)
y = np.sin(x)
plt.plot(x, y, marker="x")
I am receiving the error "AttributeError: module 'matplotlib' has no attribute 'plot'" Any help would be appreciated.
Have you installed matplotlib properly? I added an extra line to your code to show the plot. This code works properly in Visual Studio after installing the matplotlib library.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10 , 10, 100)
y = np.sin(x)
plt.plot(x, y, marker="x")
plt.show()
Try this simple step.
use pyplot using below import statement when importing matplotlib library.
import matplotlib.pyplot as plt
So I'm trying to export some plots I created using matplotlib and seaborn.
I create my plot with:
import pandas as pd
import matplotlib as plt
import matplotlib.pyplot as plt
from matplotlib import pyplot as plty
import seaborn as sns
%matplotlib inline
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('png', 'pdf')
df = pd.read_excel('test.xlsx', sheetname='IvT')
sns.set_style("white")
plt.figure(figsize=(12,10))
plt.xlabel('Test', fontsize=18)
plt.title ('Test', fontsize=22)
#sns.boxplot(df[['Short total']])
sns.boxplot(df[['Short total']])
plt.show()
If I try to export it with
matplotlib.pyplot.savefig("test.svg", format="svg")
I get an error message saying
--------------------------------------------------------------------------- NameError Traceback (most recent call
last) in ()
1
----> 2 matplotlib.pyplot.savefig("test.svg", format="svg")
NameError: name 'matplotlib' is not defined
You seem to be overcomplicating the importing of modules. In the code, you have imported matplotlib and matplotlib.pyplot as plt. In addition, having already imported matplotlib.pyplot, you try and do it again using from matplotlib import pyplot
When you try and save your file you have then done matplotlib.pyplot.savefig, but you have already imported matplotlib.pyplot as plt.
The specific error you have shown is because, while you have import matplotlib itself, you have imported it as plt, which is why the error says that matplotlib is not defined.
In order to fix this, you need to clean up the imports like so:
import pandas as pd
import matplotlib # if abbreviating this, use "as mpl"
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('png', 'pdf')
df = pd.read_excel('test.xlsx', sheetname='IvT')
sns.set_style("white")
plt.figure(figsize=(12,10))
plt.xlabel('Test', fontsize=18)
plt.title ('Test', fontsize=22)
#sns.boxplot(df[['Short total']])
sns.boxplot(df[['Short total']])
plt.show()
Then in order to save your figure use:
plt.savefig("test.svg", format="svg")
Remember to call this before plt.show()
I'm trying to make a plot:
from matplotlib import *
import sys
from pylab import *
f = figure ( figsize =(7,7) )
But I get this error when I try to execute it:
File "mratio.py", line 24, in <module>
f = figure( figsize=(7,7) )
TypeError: 'module' object is not callable
I have run a similar script before, and I think I've imported all the relevant modules.
The figure is a module provided by matplotlib.
You can read more about it in the Matplotlib documentation
I think what you want is matplotlib.figure.Figure (the class, rather than the module)
It's documented here
from matplotlib import *
import sys
from pylab import *
f = figure.Figure( figsize =(7,7) )
or
from matplotlib import figure
f = figure.Figure( figsize =(7,7) )
or
from matplotlib.figure import Figure
f = Figure( figsize =(7,7) )
or to get pylab to work without conflicting with matplotlib:
from matplotlib import *
import sys
import pylab as pl
f = pl.figure( figsize =(7,7) )
You need to do:
matplotlib.figure.Figure
Here,
matplotlib.figure is a package (module), and `Figure` is the method
Reference here.
So you would have to call it like this:
f = figure.Figure(figsize=(7,7))
To prevent future errors regarding matplotlib.pyplot, try doing this:
import matplotlib.pyplot as plt
If you use Jupyter notebooks and use: %matplotlib inline, make sure that the "%matplotlib inline FOLLOWS the import matplotlib.pyplot as plt
Karthikr's answer works but might not eliminate errors in other lines of code related to the pyplot module.
Happy coding!!
Instead of
from matplotlib import *
use
import matplotlib.pyplot as plt
For Jupyter notebook I solved this issue by importig matplotlib.pyplot instead.
import matplotlib.pyplot as plt
%matplotlib notebook
Making plots with f = plt.figure() now works.
Wrong Library:
import matplotlib as plt
plt.figure(figsize=(7, 7))
TypeError: 'module' object is not callable
But
Correct Library:
import matplotlib.pyplot as plt
plt.figure(figsize=(7, 7))
Above code worked for me for the same error.