id,node_id,number,title,user,state,locked,assignee,milestone,comments,created_at,updated_at,closed_at,author_association,active_lock_reason,draft,pull_request,body,reactions,performed_via_github_app,state_reason,repo,type 697814535,MDU6SXNzdWU2OTc4MTQ1MzU=,4414,Enhanced plotting option (latex backend and/or unit change),30407294,closed,0,,,5,2020-09-10T11:32:15Z,2020-09-10T15:58:29Z,2020-09-10T14:30:04Z,NONE,,,,"One problem with `xarray`'s default plotting functionality is that the plots look quite mediocre, and so if you want to present your results, then you may have to go in and change all the defaults, and relabel the graphs. This seems like wasted effort. `xarray` could support `pdflatex` backend usage by `matplotlib` by sanitizing the labels. It could also change units from `degrees_north` to the more presentable`$^{\circ}$N`. An example of doing this is shown below: ``` import numpy as np import re import datetime import matplotlib import matplotlib.pyplot as plt import xarray as xr def mpl_params(quality='high'): """""" Apply my plotting style to produce nice looking figures. Call this at the start of a script which uses matplotlib, and choose the correct setting. :return: """""" if quality == 'high': matplotlib.style.use('seaborn-colorblind') param_set = {""pgf.texsystem"": ""pdflatex"", ""text.usetex"": True, ""font.family"": ""serif"", ""font.serif"": [], ""font.sans-serif"": [""DejaVu Sans""], ""font.monospace"": [], ""lines.linewidth"": 0.75, ""pgf.preamble"": [r""\usepackage[utf8x]{inputenc} \usepackage[T1]{fontenc}""] } else: matplotlib.style.use('seaborn-colorblind') param_set = {""text.usetex"": False, ""lines.linewidth"": 0.75, ""font.family"": ""sans-serif"", ""font.serif"": [], ""font.sans-serif"": [""DejaVu Sans""], } matplotlib.rcParams.update(param_set) def tex_escape(text): """""" It is better to plot in TeX, but this involves escaping strings. from: https://stackoverflow.com/questions/16259923/ how-can-i-escape-latex-special-characters-inside-django-templates :param text: a plain text message :return: the message escaped to appear correctly in LaTeX # removed unicode(key) from re.escape because this seemed an unnecessary, and was throwing an error. """""" conv = { '&': r'\&', '%': r'\%', '$': r'\$', '#': r'\#', '_': r'\_', '{': r'\{', '}': r'\}', '~': r'\textasciitilde{}', '^': r'\^{}', '\\': r'\textbackslash{}', '<': r'\textless{}', '>': r'\textgreater{}', } regex = re.compile('|'.join(re.escape(key) for key in sorted(conv.keys(), key=lambda item: - len(item)))) return regex.sub(lambda match: conv[match.group()], text) def proper_units(text): conv = { 'degC': r'$^{\circ}$C', 'degrees\_celsius': r'$^{\circ}$C', 'degrees\_north': r'$^{\circ}$N', 'degrees\_east': r'$^{\circ}$E', } regex = re.compile('|'.join(re.escape(key) for key in sorted(conv.keys(), key=lambda item: - len(item)))) return regex.sub(lambda match: conv[match.group()], text) mpl_params(quality='high') ds = xr.tutorial.load_dataset('air_temperature') plot_data = ds.air.isel(time=120) plot = plot_data.plot(center=273.15, cbar_kwargs={'label': 'K'}) plt.title(tex_escape('time = '+ np.datetime_as_string(plot_data.coords['time'].data, unit='D'))) plt.xlabel(proper_units(tex_escape(ds.coords['lon'].long_name + ' [' + ds.coords['lon'].units + ']'))) plt.ylabel(proper_units(tex_escape(ds.coords['lat'].long_name + ' ['+ ds.coords['lat'].units + ']'))) plt.show() ``` ","{""url"": ""https://api.github.com/repos/pydata/xarray/issues/4414/reactions"", ""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,completed,13221727,issue