I get this error when I try to save my plot with matplotlib ( plt.save('static/chart.png')). I am sure there is a problem with import but I am not sure what exactly. These are imports I use:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
There is no save function in matplotlib.pyplot. ×Perhaps you want to use savefig?
Related
This is my first time asking a question. I am trying to use "pysheds" to analyze some hydrologic DEM files. The developer as some pretty thorough "how to" videos but when I try to load the DEM file in the way that they show I get the following error:
module 'pysheds.grid' has no attribute 'from_raster'
here's my code
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import geopandas as gpd
import pysheds
import pysheds.grid as Grid
import mplleaflet
grid = Grid.from_raster('path.tif', data_name = 'dem')`
I checked the print(dir(Grid)) in the console and didn't see this attribute listed.
Am I missing something?
Thanks!
According to documentation, you should import Grid from pysheds.grid like this:
from pysheds.grid import Grid
grid = Grid.from_raster('n30w100_con', data_name='dem')
grid.read_raster('n30w100_dir', data_name='dir')
grid.view('dem')
instead of
import pysheds.grid as Grid
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()
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 working on a Django project where I'd like to get data from CSV's and plot them to pages built using Django's views/ templates. Heres the code I have thus far:
import matplotlib.pyplot as plt, mpld3
graph = plt.plot([1,2,3,4])
g = mpld3.fig_to_html(graph)
return HttpResponse(g)
However when I run this I get the error:
AttributeError: 'list' object has no attribute 'savefig'
Anyone know where I went wrong, or how I can create graphs I can add into pages I already have, and not the page MPLD3 renders for you.
plt.plot returns a list of Line2D objects, not a figure object.
What you want to do is this:
import matplotlib.pyplot as plt, mpld3
fig = plt.figure()
fid=plt.plot([3,1,4,1,5])
mpld3.save_html(fig,"test.html")
mpld3.fig_to_html(fig,template_type="simple")
mpld3.show()
Like write #hck3r, plt.plot returns a list of Line2D objects, not a figure object. You need little modifications of your code:
import matplotlib.pyplot as plt, mpld3
fig = plt.figure()
plt.plot([1,2,3,4])
g = mpld3.fig_to_html(fig)
return HttpResponse(g)
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.