I'm relatively new at Python, currently running Python 3.8. I want to take advantage of the many symbols in the DejaVuSans (default font for Matplotlib). Rather than have users of my program type in the entire unicode sequence (e.g., "$\0041$" to plot the letter A) I was wanting them only to record 0041 and I'd build the full code programmatically. It's not working, perhaps a newbie mistake. The code included is sample code from Matplotlib Basemap, and I'm just changing the parameter to set up the marker. I have a print statement, that shows the variable (mymarker) looks like I've built it correctly, but it fails. If I replace mymarker variable and with marker="$\0041$" the program runs fine.enter image description here
from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt map = Basemap(projection='ortho', lat_0=0, lon_0=0) map.drawmapboundary(fill_color='aqua') map.fillcontinents(color='coral',lake_color='aqua') map.drawcoastlines() lons = [0, 10, -20, -20] lats = [0, -10, 40, -20] x, y = map(lons, lats) unicod_sym = "0041" mymarker="\"$\\"+unicod_sym+"$\"" print (mymarker) map.scatter(x, y, marker=mymarker,s=125, color = 'blue', linewidth=.5) plt.show() ============================= RESTART: C:/Users/dbwal/Documents/Code/Python/LearningPython/basemap_scatter_testunicodes.py ============================= "$\0041$" Traceback (most recent call last): File "C:\Users\dbwal\anaconda3\lib\site-packages\matplotlib\mathtext.py", line 2617, in parse result = self._expression.parseString(s) File "C:\Users\dbwal\anaconda3\lib\site-packages\pyparsing.py", line 1955, in parseString raise exc File "C:\Users\dbwal\anaconda3\lib\site-packages\matplotlib\mathtext.py", line 2775, in unknown_symbol raise ParseFatalException(s, loc, "Unknown symbol: %s" % c) pyparsing.ParseFatalException: Unknown symbol: , found '' (at char 0), (line:1, col:1)
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "C:/Users/dbwal/Documents/Code/Python/LearningPython/basemap_scatter_testunicodes.py", line 22, in map.scatter(x, y, marker=mymarker,s=125, color = 'blue', linewidth=.5) File "C:\Users\dbwal\anaconda3\lib\site-packages\mpl_toolkits\basemap_init_.py", line 564, in with_transform return plotfunc(self,x,y,*args,**kwargs) File "C:\Users\dbwal\anaconda3\lib\site-packages\mpl_toolkits\basemap_init_.py", line 3262, in scatter ret = ax.scatter(*args, **kwargs) File "C:\Users\dbwal\anaconda3\lib\site-packages\matplotlib_init_.py", line 1438, in inner return func(ax, *map(sanitize_sequence, args), **kwargs) File "C:\Users\dbwal\anaconda3\lib\site-packages\matplotlib\cbook\deprecation.py", line 411, in wrapper return func(*inner_args, **inner_kwargs) File "C:\Users\dbwal\anaconda3\lib\site-packages\matplotlib\axes_axes.py", line 4473, in scatter marker_obj = mmarkers.MarkerStyle(marker) File "C:\Users\dbwal\anaconda3\lib\site-packages\matplotlib\markers.py", line 228, in init self.set_marker(marker) File "C:\Users\dbwal\anaconda3\lib\site-packages\matplotlib\markers.py", line 313, in set_marker self._recache() File "C:\Users\dbwal\anaconda3\lib\site-packages\matplotlib\markers.py", line 241, in _recache self._marker_function() File "C:\Users\dbwal\anaconda3\lib\site-packages\matplotlib\markers.py", line 395, in _set_mathtext_path text = TextPath(xy=(0, 0), s=self.get_marker(), File "C:\Users\dbwal\anaconda3\lib\site-packages\matplotlib\textpath.py", line 397, in init self._vertices, self._codes = text_to_path.get_text_path( File "C:\Users\dbwal\anaconda3\lib\site-packages\matplotlib\textpath.py", line 128, in get_text_path glyph_info, glyph_map, rects = self.get_glyphs_mathtext(prop, s) File "C:\Users\dbwal\anaconda3\lib\site-packages\matplotlib\textpath.py", line 185, in get_glyphs_mathtext width, height, descent, glyphs, rects = self.mathtext_parser.parse( File "C:\Users\dbwal\anaconda3\lib\site-packages\matplotlib\mathtext.py", line 3340, in parse return self._parse_cached( File "C:\Users\dbwal\anaconda3\lib\site-packages\matplotlib\mathtext.py", line 3364, in _parse_cached box = self._parser.parse(s, font_output, fontsize, dpi) File "C:\Users\dbwal\anaconda3\lib\site-packages\matplotlib\mathtext.py", line 2619, in parse raise ValueError("\n".join(["", ValueError: \0041 ^ Unknown symbol: , found '' (at char 0), (line:1, col:1)
https://stackoverflow.com/questions/66502513/need-help-in-creating-a-valid-unicode-to-create-a-point-marker-in-matplotlib March 06, 2021 at 12:53PM
没有评论:
发表评论