I created a scatter plot on indian map in jupyter notebook but when i am trying to run the same code in my djnago app. It raises
ModuleNotFoundError: No module named 'mpl_toolkits.basemap'
Here's the code:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
# make up some data for scatter plot
lats = np.random.randint(6, 37, size=50)
lons = np.random.randint(68, 97, size=50)
fig = plt.gcf()
fig.set_size_inches(8, 6.5)
m = Basemap(projection='cyl', \
llcrnrlat=6., urcrnrlat=37., \
llcrnrlon=68., urcrnrlon=97., \
lat_ts=20, \
resolution='c',epsg=3857)
m.bluemarble(scale=1)
m.drawcoastlines(color='white', linewidth=0.2)
m.drawmapboundary(fill_color='#D3D3D3')
x, y = m(lons, lats)
plt.scatter(x, y, 10, marker='o', color='Red')
plt.show()
I am using the same conda interpreter in my django app. whats is the reason for this error ?
I use Anaconda under Win10. With a different OS or installation the solution may be different for you, but I can try to sketch the path.
You have to install Basemap. See here and here. For me conda install -c conda-forge basemap-data-hires did work.
When importing from mpl_toolkits.basemap import Basemap I got an error: KeyError: PROJ_LIB. Following the experiences here you must find a directory share with epsg inside. It is expected to be in the conda or the anaconda directory.
I did a search in my Anaconda3-dirctory and have found this path: "C:\Anaconda3\Library\share"
Then the following code did work for me (and your code gives a nice picture too :-) :
import os
proj_lib = "C:\Anaconda3\Library\share"
os.environ["PROJ_LIB"] = proj_lib
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
plt.figure(figsize=(8, 8))
m = Basemap(projection='ortho', resolution=None, lat_0=50, lon_0=-100)
m.bluemarble(scale=0.5);
Related
i am trying to learn machine learn through Python from W3School. I am trying to get mydecisiontree. PNG using PyDotPlus
I am using pip PyCharm professional 2020.3
the code is as follow:
import numpy as np
import matplotlib.pyplot as plt
import pandas
from sklearn import tree
import pydotplus
from sklearn.tree import DecisionTreeClassifier
import matplotlib.image as pltimg
df = pandas.read_csv("shows.csv")
d = {'UK' : 0,'USA' : 1, 'N' : 2}
df['Nationality'] = df['Nationality'].map(d)
d = {'YES' : 1, "NO" : 0}
df['Go'] = df['Go'].map(d)
features = ['Age', 'Experience', 'Rank', 'Nationality']
X = df[features]
y = df['Go']
dtree = DecisionTreeClassifier()
dtree = dtree.fit(X, y)
data = tree.export_graphviz(dtree, out_file=None, feature_names=features)
graph = pydotplus.graph_from_dot_data(data)
graph.write_png('mydecisiontree.png')
img=pltimg.imread('mydecisiontree.png')
img_plot = plt.imshow(img)
plt.show()
Although the PyCharm shows no error but when i run the code it cant make the PNG file and gives an error on the line:
graph.write_png('mydecisiontree.png')
It shows the following error:
File "direc.....\venv\lib\site-packages\pydotplus\graphviz.py", line 1960, in create
'GraphViz\'s executables not found')
pydotplus.graphviz.InvocationException: GraphViz's executables not found
I can't see the problem. How to solve this?
PyCharm doesn't show an error because your code doesn't contain any. The problem is in your environment. Have you installed GraphViz (using pip install graphviz) in the environment that you are using to run it?
Also see the answers here:
GraphViz not working when imported inside PydotPlus (GraphViz's executables not found)
Graphviz's executables are not found (Python 3.4)
I had that problem too,
I installed graphviz from: here
and it solved.
When I run this program in Python, it show me this error:
ImportError: No module named skimage.io.
I have already run the command pip install scikit-image, but I still get this error. Can you please help me?
This is my code:
import matplotlib.pyplot as plt
import skimage.io as io
import skimage.color as color
parrots = io.imread('C:\Python27\Example\parrots.bmp')
parrots_hsv= skcolor.convert_colorspace(parrots, 'RGB','HSV')
fig, ax= plt.subplots(nclos = 2, figsize=('8,4'))
ax[0].imshow(parrots)
ax[0].set_title('original image')
restored_image =skcolore.convert_colorspace(parrots_hsv, 'HSV','RGB')
ax[1].imshow(restored_image)
ax[1].set_title('restored image')
plt.show()`enter code here`
I reproduced this error like so:
import math.sqrt as sq
print(sq(4))
Error:
File ".\Solution.py", line 1, in <module>
import math.sqrt as sq
ModuleNotFoundError: No module named 'math.sqrt'; 'math' is not a package
repaired by:
from math import sqrt as sq
print(sq(4))
So I presume if you changed your imports to the below code it might fix it:
from matplotlib import pyplot as plt
from skimage import io as io
from skimage import color as color
In addition, you might want to read a bit here for a simple explanation about imports.
I have a problem with matplotlib in Pycharm.my code is:
import matplotlib.pyplot as plt
import numpy as np
T = np.linspace(10,100,10)
es = 611*np.exp(17.27*T/(237.3+T))
plt.plot(T,es)
plt.xlabel('T (degree Celcius)')
plt.ylabel('es (pa)')
plt.show()
and after i ran this code,Pycharm showed this error:
enter link description here
what should i do to solve it??
I want to save my plot with font from Latex, but I have error:
TypeError: a bytes-like object is required, not 'str'
I initialize Latex in pyplot by:
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
And save pdf by:
fig.savefig('myplot.pdf', transparent=True)
Saving all to png works, only pdf failed. Any ideas?
Try importing Pdfpages from matplotlib and implement as following:
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
fig = plt.figure()
pdf = PdfPages('foo.pdf')
pdf.savefig(fig)
pdf.close()
For me, the solution from here worked.
Adjust your matplotlib script by adding the following lines after
import matplotlib:
matplotlib.use("pgf")
matplotlib.rcParams.update({
"pgf.texsystem": "pdflatex",
'font.family': 'serif',
'text.usetex': True,
'pgf.rcfonts': False,
})
I am trying to run this code.
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('games.jpg',0)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([])
plt.show()
But I keep getting this error
Traceback (most recent call last):
File "mpl.py", line 3, in <module>
from matplotlib import pyplot as plt
File "/home/megha/matplotlib.py", line 3, in <module>
from matplotlib import pyplot as plt
ImportError: cannot import name pyplot
I googled a solution for this but I am only getting that the matplotlib version needs to be upgraded. I tried that too, and its still showing the same error.
I have also tried
import matplotlib.pyplot as plt but I have python 2.7 version, so that didnt work either.
Use import matplotlib.pyplot as plt instead.