Axes3d vs axes3d matplotlib - python

I just finished a horrible install of scipy, numpy, and matplotlib on OSX Lion. For some reason, I can't do a:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3d
Error output is:
Traceback (most recent call last):
File "3dPlot.py", line 2, in <module>
from mpl_toolkits.mplot3d import Axes3d
ImportError: cannot import name Axes3d
but I can do:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
Is there a different between Axes3d and axes3d, or is my file just named with a lowercase 'a' for some reason?

The following two things work for me (watch your capitalization):
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import axes3d
But I get the same error for what you wrote (with the d in 3d in lower case). Note that the two imports that do work are not equivalent.

from mpl_toolkits.mplot3d import Axes3D
Please use above command to use the 3D image for an object

Related

Import Issue with Matplotlib and Pyplot (Python / Tkinter)

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()

Using Matplotlib for plotting 3D data - warning/error importing module [duplicate]

When using the typical 3D plot like so:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
flake8 reports the expected error:
./tools.py:62:9: F401 'mpl_toolkits.mplot3d.Axes3D' imported but unused
I know it can be avoided using the # NOQA comment. But is there a different way to formulate the projection in the figure so that the Axes3D object is used?
If this is only about actually using the import at least once, you can do
ax = fig.gca(projection=Axes3D.name)
as "3d" is the name of the Axes3D class by which it is registered to the projection list.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
However, if I understand the documentation well, it is not the preferred way anymore since version 1.0.0. I still mention it for completeness.

Exporting SVG files from plots created in Python

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()

How to directly use Axes3D from matplotlib in standard plot to avoid flake8 error

When using the typical 3D plot like so:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
flake8 reports the expected error:
./tools.py:62:9: F401 'mpl_toolkits.mplot3d.Axes3D' imported but unused
I know it can be avoided using the # NOQA comment. But is there a different way to formulate the projection in the figure so that the Axes3D object is used?
If this is only about actually using the import at least once, you can do
ax = fig.gca(projection=Axes3D.name)
as "3d" is the name of the Axes3D class by which it is registered to the projection list.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
However, if I understand the documentation well, it is not the preferred way anymore since version 1.0.0. I still mention it for completeness.

Python "'module' object is not callable"

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.

Categories