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,
})
Related
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()
How can I save plot from mglearn? I tried this code but it does not work.
import numpy as np
import matplotlib.pyplot as plt
import mglearn
from fpdf import FPDF
f = mglearn.plots.plot_knn_classification(n_neighbors=3)
f.savefig("n_neighbors.pdf", bbox_inches='tight')
The error was AttributeError: 'NoneType' object has no attribute 'savefig'.
Thanks
As the mglearn package uses matplotlib.pyplot's plotting mechanics you can use their saving mechanics, like so.
mglearn.plots.plot_knn_classification(n_neighbors=3)
plt.savefig("n_neighbors.pdf", bbox_inches='tight')
from sklearn.neighbors import KNeighborsClassifier
import matplotlib.pyplot as plt
import mglearn.plots
import numpy as np
import mglearn
from fpdf import FPDF
X, y = mglearn.datasets.make_forge()
mglearn.plots.plot_knn_classification(n_neighbors=3)
plt.savefig("n_neighbors.pdf", bbox_inches='tight')
This code work.
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 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 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.