I keep getting error which is wordcloud is not defined.
I am not sure where is my mistake on the script.
# Display your wordcloud image
wordcloud = WordCloud(max_font_size=40, relative_scaling=.5).generate(text)
plt.figure()
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
#Error: name 'WordCloud' is not defined
WordCloud is not a default class in Python. Assuming you have already installed the Python package, you are going to have to import it.
from wordcloud import WordCloud #This line here
wordcloud = WordCloud(max_font_size=40, relative_scaling=.5).generate(text)
plt.figure()
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
Related
I am trying to replicate a simple object detection that I found in on website.
import cv2
import matplotlib.pyplot as plt
import cvlib as cv
from cvlib.object_detection import draw_bbox
im = cv2.imread('downloads.jpeg')
bbox, label, conf = cv.detect_common_objects(im)
output_image = draw_bbox(im, bbox, label, conf)
plt.imshow(output_image)
plt.show()
All required libraries are installed and there are no errors running the code. However, it does not show the output image with the boxes, labels and confidence. How do I fix it?
#After loading an image use an assert:
img = cv2.imread('downloads.jpeg')
assert not isinstance(img,type(None)), 'image not found'
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);
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,
})
Wrote this simple little script to create a wordcloud from a transcript of the Benghazi hearing today, but it's getting hung up on an error I don't really understand.
from os import path
from wordcloud import WordCloud
d = path.dirname(__file__)
# Read the whole text.
text = open(path.join(d, 'clintonSpeech.txt')).read()
# Generate a word cloud image
wordcloud = WordCloud().generate(text)
# Display the generated image:
# the matplotlib way:
import matplotlib.pyplot as plt
plt.imshow(wordcloud)
plt.axis("off")
# take relative word frequencies into account, lower max_font_size
wordcloud = WordCloud(max_font_size=40, relative_scaling=.5).generate(text)
plt.figure()
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
Traceback:
Traceback (most recent call last):
File "wordCloud.py", line 19, in <module>
wordcloud = WordCloud(max_font_size=40, relative_scaling=.5).generate(text)
TypeError: __init__() got an unexpected keyword argument 'relative_scaling'
To fix this, change
wordcloud = WordCloud(max_font_size=40, relative_scaling=.5).generate(text)
to
wordcloud = WordCloud(max_font_size=40).generate(text)
Figured it out as I was typing this up, but will leave it up as maybe someone else will have the same problem.
I am trying to run the following Statsmodels example from http://statsmodels.sourceforge.net/devel/examples/notebooks/generated/tsa_arma_0.html.
fig, ax = plt.subplots(figsize=(12, 8))
ax = dta.ix['1950':].plot(ax=ax)
fig = arma_mod30.plot_predict('1990', '2012', dynamic=True, ax=ax, plot_insample=False)
Running the code above gives the error message below. Even after upgrading to Statsmodels 6, I am getting the same error.
AttributeError Traceback (most recent call last)
<ipython-input-69-2a5da9c756f0> in <module>()
1 fig, ax = plt.subplots(figsize=(12, 8))
2 ax = dta.ix['1950':].plot(ax=ax)
----> 3 fig = arma_mod30.plot_predict('1990', '2012', dynamic=True, ax=ax, plot_insample=False)
C:\Anaconda\lib\site-packages\statsmodels\base\wrapper.pyc in __getattribute__(self, attr)
33 pass
34
---> 35 obj = getattr(results, attr)
36 data = results.model.data
37 how = self._wrap_attrs.get(attr)
AttributeError: 'ARMAResults' object has no attribute 'plot_predict'
Any suggestions?
This issue has been resolved after following the below comment. Thanks.
I've just encountered the same issue with statsmodels 0.13.2. After a bit of digging in their release notes I can see that the plotting functionality has been separated out. Instead of
arma_mod30.plot_predict(...)
try
from statsmodels.graphics.tsaplots import plot_predict
plot_predict(arma_mod30, ...)
Hope this helps
I also meet this issue for me the #pip install statsmodels==0.11.0 version get solved this promblem. I get this error after updating all the python packages. GL
Maybe it is the version of statsmodels made that happen. Try to check the version of statsmodels before upgrade the package to 0.6.1
>>> import statsmodels
>>> statsmodels.__version__
$ pip install statsmodels --upgrade
For more information, click this issue on statsmodels.github