LaTeX was not able to process the following string: b'lp' - python-3.x

I just begin with Python, scypi and matplotlib, I had copy this code:
from scipy import stats
import numpy as np
import matplotlib.pyplot as plt
data = stats.exponweib.rvs(a=1, c=2.09, scale=10.895, loc=0, size=2500)
plt.plot(data, stats.exponweib.pdf(data, *stats.exponweib.fit(data, 1, 1, scale=02, loc=0))
_ = plt.hist(data, bins = np.linspace(0, 16, 33), normed=True, alpha=0.5)
plt.show()
But it show an error:
'LaTeX was not able to process the following string: b'lp''
The file ...Lib\site-packages\matplotlib\mpl-data\matplotlibrc show:
#text.usetex : False
os : windows 7
dis: winpython
What can I do?
Thanks.

Related

Numpy in VS Code Ubuntu 20.04

I am new to Ubuntu and is trying to get Numpy working on VS Code. Following is my code:
import matplotlib.pyplot as plt
import numpy as np
x = [1,2,3,4]
y = [3,5,7,9]
plt.grid(True)
plt.xlabel("My X values")
plt.ylabel("My Y values")
plt.plot(x,y, 'b-^', lineWidth = 3, markersize = 7, label = "Blue Line")
plt.legend(loc = "upper center")
plt.show()
But the debugger gives me the following error:
No module named 'numpy.core._multiarray_umath'
Does anyone knows how to install that module? Thank you so much in advance.

read an svg file to insert in matplotlib

I am trying to read an image from an SVG file and insert in matplotlib figure.
import matplotlib.pyplot as plt
import pylustrator as pyl
import numpy as np
from matplotlib.figure import Figure
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
from svglib.svglib import svg2rlg
ax = plt.subplot(111)
ax.plot(
[1, 2, 3], [1, 2, 3],
'go-',
label='line 1',
linewidth=2
)
# arr_img = plt.imread("stinkbug.svg")
# arr_img = svg2rlg("stinkbug.svg")
arr_img = pyl.load("stinkbug.svg")
im = OffsetImage(arr_img)
ab = AnnotationBbox(im, (1, 0), xycoords='axes fraction')
ax.add_artist(ab)
plt.show()
I tried to use pylustratorpost for reading the svg image. The code works when the input image is in png format. But I am not able to add the same image saved in svg extension(image).
I couldn't successfully insert due to the following error,
"float".format(self._A.dtype))
TypeError: Image data of dtype object cannot be converted to float
Suggestions on how to fix this will be really helpful.

How to plot mode as a line using Python, Numpy, Matplotlib?

[Image here]
1I want to plot mode of as a line that comes from a bunch of lines. But I get value error as follows:
ValueError: x and y must have same first dimension, but have shapes (1, 159) and (2, 1, 159)
How to solve it?
My Code is as follows:
from glob import glob
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from scipy import stats
hvsra = []
for filename in glob('*.hv'):
with open(filename) as f:
hv = np.genfromtxt(f)
hv_m = np.ma.array(hv)
new_hv = hv_m[:,0:2]
freq = new_hv[:,0]
freq_new = np.reshape(freq_arr, (1, 159))
amp = new_hv[:,1]
hvsra.append(amp)
hvsr = np.array(hvsra)
hvsrm = stats.mode(hvsr)
plt.figure(figsize=(12, 8))
plt.loglog(freq_new,
hvsrm)
Thanks for your help.

Multiple plot single colorbar in python matplotlib basemap system crash problem

Why I am getting a runtime error while running this code?? I wanted to show two SST data in a single plot using for loop. while I am running the code it shows your system has crashed. I am sharing the screenshot of the system log.
Screenshot of the error
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from netCDF4 import Dataset
from mpl_toolkits.axes_grid1 import ImageGrid
def read_sst (x):
ncfile=x
fh=Dataset(ncfile,mode='r')
sst=fh.variables['sst'][:]
sst_anom=sst[0,:,:]
return sst_anom
sst_anom_drought=read_sst("Drought_SST_Mean_Anomaly_Composite_1901-2014.nc")
sst_anom_flood=read_sst("Flood_SST_Mean_Anomaly_Composite_1901-2014.nc")
SST_ncfile='Flood_SST_Mean_Anomaly_Composite_1901-2014.nc'
fh1=Dataset(SST_ncfile,mode='r')
lons = fh1.variables['longitude'][:]
lats = fh1.variables['latitude'][:]
sst_data=[sst_anom_drought,sst_anom_flood]
fig = plt.figure(1,(15.,5.))
grid_top = ImageGrid(fig, 111, nrows_ncols = (2, 1),axes_pad=0.5)
cbar_ax = fig.add_axes([0.25,0.15, 0.55, 0.015])
for g, s in zip(grid_top,sst_data):
plt.sca(g)
m = Basemap(resolution='l',projection='merc',llcrnrlon=30, llcrnrlat=-31,
urcrnrlon=360, urcrnrlat=65)
clevs = np.linspace(-0.8, 0.8, 15)
lons, lats = np.meshgrid(lons, lats)
xi, yi = m(lons, lats)
color_map = plt.cm.RdBu_r
#reversed_color_map = color_map.reversed()
cs = m.contourf(xi,yi,s,clevs,cmap=color_map,extend='both')
cb=fig.colorbar(cs,orientation="horizontal",cax=cbar_ax)
m.drawparallels(np.arange(-30., 65., 15.), labels=[1,0,0,0], fontsize=12,linewidth=0.1)
m.drawmeridians(np.arange(-180., 180., 40.), labels=[0,0,0,1], fontsize=12,linewidth=0.1)
m.fillcontinents(color='whitesmoke',lake_color='whitesmoke')
m.drawcoastlines()

connect two points of a plot with another line

I want to connect two points in a data frame plot with another line and add it to the plot:
import numpy as np
from numpy.random import randn
import pandas as pd
from datetime import datetime
import matplotlib.pyplot as plt
%matplotlib inline
days = [datetime(2016, 1, 1), datetime(2016, 1, 2),datetime(2016, 1, 3),datetime(2016, 1, 4)]
dt_ind = pd.DatetimeIndex(days)
data = np.random.randn(4,2)
cols = ['A','B']
df = pd.DataFrame(data,dt_ind,cols)
df['A'].plot(figsize=(12,4), sort_columns=True)
here is the data frame:
enter image description here
and the plot:
enter image description here
how is that possible? for example add a line from point 2 to point 4 (or any two points)
You want to use matplotlib's plt.subplots() function to return a fig and ax object, so you can then add separate lines to your ax.
import numpy as np
from numpy.random import randn
import pandas as pd
from datetime import datetime
import matplotlib.pyplot as plt
%matplotlib inline
days = [datetime(2016, 1, 1),
datetime(2016, 1, 2),
datetime(2016, 1, 3),
datetime(2016, 1, 4)]
dt_ind = pd.DatetimeIndex(days)
data = np.random.randn(4,2)
cols = ['A','B']
df = pd.DataFrame(data,dt_ind,cols)
fig, ax = plt.subplots()
ax.plot(df['A'], color='red')
ax.plot([df.index[1], df.index[3]],
[df['A'][1], df['A'][3]], color='blue')

Resources