matplotlib bug in plot_dates? - python

Just wanted to see if others thought that the following behaviour in matplotlib plot_date was buggy, or if it's just something I should put up with.
I have a multi-panel plot that I set up with sharex to facilitate zoom/pan on all axes, and I plot time series data in both panels. However, in the second panel, all of the data happens to be invalid (in this example I mask it).
from matplotlib.pyplot import figure,show
from datetime import datetime,timedelta
from numpy import sin,cos,linspace,pi,ma,array
fig=figure(figsize=(16,9))
ax1=fig.add_subplot(211)
ax2=fig.add_subplot(212,sharex=ax1)
# xdata is seconds
xdata=linspace(0,9999,10000)
tdata=array([datetime(2000,1,1)+timedelta(seconds=ss) for ss in xdata])
data1=ma.masked_array(sin(pi*xdata/300),mask=False)
data2=ma.masked_array(cos(pi*xdata/300),mask=True)
ax1.plot_date(tdata,data1,marker='',color='r')
ax2.plot_date(tdata,data2,marker='',color='b')
show()
I'd expect (prefer) it to just show up a blank panel, not to fail and give me a long unhelpful traceback. Is this the expected behaviour?
Notes:
This script also fails if I use ax.plot(...) instead of ax.plot_date(...)
It works fine (i.e. gives me an empty panel) if I just plot against xdata, not the datetime array tsdata (but I have to use ax1.set_xlim(xdata[0],xdata[-1]) to get a sensible domain displayed):
ax1.plot(xdata,data1,marker='',color='r')
ax2.plot(xdata,data2,marker='',color='b')
ax1.set_xlim(xdata[0],xdata[-1])
show()
I have just realised I can rescue the above plot by forcing the ax2 limits right before the show() command. I still think that the failure in the main example is inelegant:
ax2.set_xlim(tdata[0],tdata[-1])
show()
What do the experts think?
Thanks!
F.Y.I., This was on matplotlib 1.1.0, compiled from source on my PC.
Here is the traceback that I get:
Traceback (most recent call last):
File "/home/tchubb/local/lib/python2.7/site-packages/matplotlib/backendsbackend_gtk.py", line 395, in expose_even
self._render_figure(self._pixmap, w, h)
File "/home/tchubb/local/lib/python2.7/site-packages/matplotlib/backends/backend_gtkagg.py", line 75, in _render_f
FigureCanvasAgg.draw(self)
File "/home/tchubb/local/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 401, in draw
self.figure.draw(self.renderer)
File "/home/tchubb/local/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/home/tchubb/local/lib/python2.7/site-packages/matplotlib/figure.py", line 884, in draw
func(*args)
File "/home/tchubb/local/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/home/tchubb/local/lib/python2.7/site-packages/matplotlib/axes.py", line 1983, in draw
a.draw(renderer)
File "/home/tchubb/local/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/home/tchubb/local/lib/python2.7/site-packages/matplotlib/axis.py", line 1036, in draw
ticks_to_draw = self._update_ticks(renderer)
File "/home/tchubb/local/lib/python2.7/site-packages/matplotlib/axis.py", line 926, in _update_ticks
tick_tups = [ t for t in self.iter_ticks()]
File "/home/tchubb/local/lib/python2.7/site-packages/matplotlib/axis.py", line 873, in iter_ticks
majorLocs = self.major.locator()
File "/home/tchubb/local/lib/python2.7/site-packages/matplotlib/dates.py", line 749, in __call__
self.refresh()
File "/home/tchubb/local/lib/python2.7/site-packages/matplotlib/dates.py", line 758, in refresh
dmin, dmax = self.viewlim_to_dt()
File "/home/tchubb/local/lib/python2.7/site-packages/matplotlib/dates.py", line 530, in viewlim_to_dt
return num2date(vmin, self.tz), num2date(vmax, self.tz)
File "/home/tchubb/local/lib/python2.7/site-packages/matplotlib/dates.py", line 289, in num2date
if not cbook.iterable(x): return _from_ordinalf(x, tz)
File "/home/tchubb/local/lib/python2.7/site-packages/matplotlib/dates.py", line 203, in _from_ordinalf
dt = datetime.datetime.fromordinal(ix)
ValueError: ordinal must be >= 1
If you suspect this is an IPython bug, please report it at:
https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev#scipy.org

Yes, I would call this a bug, or at least an oversight which looks like it has been fixed (via #ali_m https://github.com/matplotlib/matplotlib/issues/162).
I was getting this exception, but that was on a version of 1.3.x from June (my bad, thought I had a more current version on this computer). Current master does not have this problem and #ali_m reports that it also works on 1.2.1 and 1.3.0 so I suspect the fix is to upgrade you version of matplotlib.
What looks like is going on is that there isn't a check in the code that sorts out where to put the ticks has no check to make sure you have given in non-empty data.
The reason that setting the limits explicitly prevents the error is that the code never tries to figure out what the range of your empty data is.
Please create a github issue for this (and include the trace back in the issue).
In [7]: Traceback (most recent call last):
File "/home/tcaswell/local_installs/lib/python2.7/site-packages/matplotlib-1.3.x-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4.py", line 366, in idle_draw
self.draw()
File "/home/tcaswell/local_installs/lib/python2.7/site-packages/matplotlib-1.3.x-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4agg.py", line 148, in draw
FigureCanvasAgg.draw(self)
File "/home/tcaswell/local_installs/lib/python2.7/site-packages/matplotlib-1.3.x-py2.7-linux-x86_64.egg/matplotlib/backends/backend_agg.py", line 440, in draw
self.figure.draw(self.renderer)
File "/home/tcaswell/local_installs/lib/python2.7/site-packages/matplotlib-1.3.x-py2.7-linux-x86_64.egg/matplotlib/artist.py", line 54, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/home/tcaswell/local_installs/lib/python2.7/site-packages/matplotlib-1.3.x-py2.7-linux-x86_64.egg/matplotlib/figure.py", line 1027, in draw
func(*args)
File "/home/tcaswell/local_installs/lib/python2.7/site-packages/matplotlib-1.3.x-py2.7-linux-x86_64.egg/matplotlib/artist.py", line 54, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/home/tcaswell/local_installs/lib/python2.7/site-packages/matplotlib-1.3.x-py2.7-linux-x86_64.egg/matplotlib/axes.py", line 2088, in draw
a.draw(renderer)
File "/home/tcaswell/local_installs/lib/python2.7/site-packages/matplotlib-1.3.x-py2.7-linux-x86_64.egg/matplotlib/artist.py", line 54, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/home/tcaswell/local_installs/lib/python2.7/site-packages/matplotlib-1.3.x-py2.7-linux-x86_64.egg/matplotlib/axis.py", line 1076, in draw
ticks_to_draw = self._update_ticks(renderer)
File "/home/tcaswell/local_installs/lib/python2.7/site-packages/matplotlib-1.3.x-py2.7-linux-x86_64.egg/matplotlib/axis.py", line 938, in _update_ticks
tick_tups = [t for t in self.iter_ticks()]
File "/home/tcaswell/local_installs/lib/python2.7/site-packages/matplotlib-1.3.x-py2.7-linux-x86_64.egg/matplotlib/axis.py", line 882, in iter_ticks
majorLocs = self.major.locator()
File "/home/tcaswell/local_installs/lib/python2.7/site-packages/matplotlib-1.3.x-py2.7-linux-x86_64.egg/matplotlib/dates.py", line 785, in __call__
self.refresh()
File "/home/tcaswell/local_installs/lib/python2.7/site-packages/matplotlib-1.3.x-py2.7-linux-x86_64.egg/matplotlib/dates.py", line 794, in refresh
dmin, dmax = self.viewlim_to_dt()
File "/home/tcaswell/local_installs/lib/python2.7/site-packages/matplotlib-1.3.x-py2.7-linux-x86_64.egg/matplotlib/dates.py", line 560, in viewlim_to_dt
return num2date(vmin, self.tz), num2date(vmax, self.tz)
File "/home/tcaswell/local_installs/lib/python2.7/site-packages/matplotlib-1.3.x-py2.7-linux-x86_64.egg/matplotlib/dates.py", line 305, in num2date
return _from_ordinalf(x, tz)
File "/home/tcaswell/local_installs/lib/python2.7/site-packages/matplotlib-1.3.x-py2.7-linux-x86_64.egg/matplotlib/dates.py", line 208, in _from_ordinalf
dt = datetime.datetime.fromordinal(ix)
ValueError: ordinal must be >= 1

Related

Setting the linestyle for the longitude and latitude lines in matplotlib's Basemap?

I am using maplotlib's Basemap to draw maps of the world and want to include longitude and latitude lines. This can be done using drawmeridians() and drawparallels(), but the linestyle of the corresponding lines can only be set via the keyword dashes. According to the documentation, see see here, is should work as follows:
dash pattern for meridians (default [1,1], i.e. 1 pixel on, 1 pixel off)
I tried dashes=[1,0] but that did not worked. Is there any simple way to have solid linestyle?
Here is my code:
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap
fig1, ax1 = plt.subplots(1,1)
map1 = Basemap( resolution='l', projection='mill',
llcrnrlat=-60., llcrnrlon=-180.,
urcrnrlat=90., urcrnrlon=180. )
map1.drawcoastlines()
map1.drawmapboundary( fill_color='aqua' )
map1.fillcontinents( color='coral', lake_color='aqua' )
# labels=[left,right,top,bottom]
map1.drawparallels( np.arange(-80.,81.,20.), labels=[True,True,False,False] )
map1.drawmeridians( np.arange(-180.,181.,40.), labels=[False,False,True,True] )
plt.show()
Here is the resulting map:
Edit 1: I just tried on a different computer and there it works, i.e. dashes=[1,0] results in solid linestyle. The version used on that computer are (according to a pip freeze)
basemap==1.2.0
matplotlib==2.2.3
As soon as I have access again to the original computer, I'll check what is going on there (and which versions are installed).
Edit 2: Being back at the computer where it did not worked, I can now tell a bit more. First, the following versions are used:
basemap==1.1.1
matplotlib==3.0.2
Then the error message (which I forgot to include previously):
ValueError: All values in the dash list must be positive
Edit 3: For the sake of completeness (and since it was partly helpful to hunt down the solution), here is the full Traceback:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1540, in __call__
return self.func(*args)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 280, in resize
self.show()
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 351, in draw
FigureCanvasAgg.draw(self)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.py", line 464, in draw
self.figure.draw(self.renderer)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/figure.py", line 1143, in draw
renderer, self, dsu, self.suppressComposite)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 2409, in draw
mimage._draw_list_compositing_images(renderer, self, dsu)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/lines.py", line 822, in draw
drawFunc(renderer, gc, tpath, affine.frozen())
File "/usr/local/lib/python2.7/dist-packages/matplotlib/lines.py", line 1267, in _draw_lines
self._lineFunc(renderer, gc, path, trans)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/lines.py", line 1297, in _draw_dashed
gc.set_dashes(self._dashOffset, self._dashSeq)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 1007, in set_dashes
raise ValueError("All values in the dash list must be positive")
After some research on some bugreports on github I found the solution [1], dashes=(None,None):
map1.drawmeridians( np.arange(-180.,181.,40.), labels=[False,False,True,True], dashes=(None,None) )
[1] https://github.com/matplotlib/basemap/issues/173#issuecomment-68243710

auto07p IndexError when uses matplotlib 2.0 instead of matplotlib 1.5

I am recently working on making auto07p work with matplotlib 2.0.
The source code can be found here:
https://sourceforge.net/projects/auto-07p/files/auto07p/0.9/
When using matplotlib 2.0 (or any version greater than 1.5), IndexError: invalid index to scalar variable. appears:
/home/ngb/auto/07p/python/Points.py:1086: VisibleDeprecationWarning: `rank` is deprecated; use the `ndim` attribute or function instead. To find the rank of a matrix see `numpy.linalg.matrix_rank`.
r = rank(array_temp)
Created plot
<graphics.windowPlotter.WindowPlotter2D object at 0x7f9e42007710>
AUTO> Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.5/tkinter/__init__.py", line 1562, in __call__
return self.func(*args)
File "/usr/lib/python3.5/tkinter/__init__.py", line 608, in callit
func(*args)
File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line 370, in idle_draw
self.draw()
File "/home/ngb/auto/07p/python/graphics/grapher_mpl.py", line 92, in draw
self.redraw()
File "/home/ngb/auto/07p/python/graphics/grapher_mpl.py", line 65, in redraw
FigureCanvasTkAgg.draw(self)
File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line 351, in draw
FigureCanvasAgg.draw(self)
File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_agg.py", line 464, in draw
self.figure.draw(self.renderer)
File "/usr/lib/python3/dist-packages/matplotlib/artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/python3/dist-packages/matplotlib/figure.py", line 1143, in draw
renderer, self, dsu, self.suppressComposite)
File "/usr/lib/python3/dist-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/lib/python3/dist-packages/matplotlib/artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/python3/dist-packages/matplotlib/axes/_base.py", line 2409, in draw
mimage._draw_list_compositing_images(renderer, self, dsu)
File "/usr/lib/python3/dist-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/lib/python3/dist-packages/matplotlib/artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/python3/dist-packages/matplotlib/axis.py", line 1136, in draw
ticks_to_draw = self._update_ticks(renderer)
File "/usr/lib/python3/dist-packages/matplotlib/axis.py", line 969, in _update_ticks
tick_tups = [t for t in self.iter_ticks()]
File "/usr/lib/python3/dist-packages/matplotlib/axis.py", line 969, in <listcomp>
tick_tups = [t for t in self.iter_ticks()]
File "/usr/lib/python3/dist-packages/matplotlib/axis.py", line 912, in iter_ticks
majorLocs = self.major.locator()
File "/usr/lib/python3/dist-packages/matplotlib/ticker.py", line 1794, in __call__
return self.tick_values(vmin, vmax)
File "/usr/lib/python3/dist-packages/matplotlib/ticker.py", line 1802, in tick_values
locs = self._raw_ticks(vmin, vmax)
File "/usr/lib/python3/dist-packages/matplotlib/ticker.py", line 1761, in _raw_ticks
istep = np.nonzero(steps >= raw_step)[0][0]
IndexError: invalid index to scalar variable.
It appears that there are only two lines involved. They are from the file /auto/07p/python/graphics/grapher_mpl.py.
The two methods involved are:
def redraw(self):
# recalculate label positions
self.grapher.plotlabels()
FigureCanvasTkAgg.draw(self)
And
def draw(self):
ax = self.grapher.ax
d = {}
if ax is self.grapher.ax3d:
[d["minx"], d["maxx"]] = ax.get_xlim3d()
[d["miny"], d["maxy"]] = ax.get_ylim3d()
[d["minz"], d["maxz"]] = ax.get_zlim3d()
d["azimuth"] = ax.azim
d["elevation"] = ax.elev
d["cur_lims"] = Axes.get_xlim(ax), Axes.get_ylim(ax)
else:
[d["minx"], d["maxx"]] = ax.get_xlim()
[d["miny"], d["maxy"]] = ax.get_ylim()
for k in list(d):
# don't adjust any unchanged settings
if k == "cur_lims":
if map(list, d[k]) == map(list, self.grapher._cur_lims):
del d[k]
elif d[k] == self.grapher.cget(k):
del d[k]
if d != {}:
if "cur_lims" in d:
del d["cur_lims"]
if d != {}:
self.grapher._configNoDraw(**d)
self.redraw()
return
FigureCanvasTkAgg.draw(self)
I would appreciate if anyone can assist me with the error, without changing the matlibplot code but only the auto code. I have read through the code but did not find anything suspicious.
I ran into this error using a given equation and auto file. But if you use matplotlib 2.0 and auto07p to plot most graphs in general, you are very likely to get this error if you zoom on the graph.
I used the files as the following:
https://1drv.ms/f/s!ArJqyyCr1FQOhkITst8JdVK_mIFV
Put them in auto folder.
Run auto.
and type:
auto("couple2.auto")
Thanks.

matplotlib: latex: savefig: OSError no such file or directory

I am generating some png figures with python 2.7.3 and Agg backend and using the options:
matplotlib.rc('font', family = 'serif', serif = 'cm10', size = 19)
matplotlib.rc('text', usetex = True)
matplotlib.rcParams['text.latex.preamble'] = [r'\boldmath']
matplotlib.rcParams['font.weight'] = 'bold'
matplotlib.rcParams['axes.linewidth'] = 2
for the labels and titles in the figures. the code works fine when I run it on my cluster on a login node. When I submit it to a computing node I get
Traceback (most recent call last):
File "main.py", line 312, in <module>
main()
File "plotvoigt.py", line 97, in main
plt.savefig(savefilename, format = 'png')
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/pyplot.py", line 696, in savefig
res = fig.savefig(*args, **kwargs)
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/figure.py", line 1563, in savefig
self.canvas.print_figure(*args, **kwargs)
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 2232, in print_figure
**kwargs)
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 527, in print_png
FigureCanvasAgg.draw(self)
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 474, in draw
self.figure.draw(self.renderer)
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/artist.py", line 62, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/figure.py", line 1159, in draw
func(*args)
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/artist.py", line 62, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 2319, in draw
a.draw(renderer)
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/artist.py", line 62, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/axis.py", line 1110, in draw
renderer)
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/axis.py", line 1060, in _get_tick_bboxes
extent = tick.label1.get_window_extent(renderer)
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/text.py", line 961, in get_window_extent
bbox, info, descent = self._get_layout(self._renderer)
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/text.py", line 352, in _get_layout
ismath=False)
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 229, in get_text_width_height_descent
renderer=self)
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/texmanager.py", line 678, in get_text_width_height_descent
page = next(iter(dvi))
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/dviread.py", line 89, in __iter__
have_page = self._read()
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/dviread.py", line 150, in _read
self._dispatch(byte)
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/dviread.py", line 233, in _dispatch
self._fnt_def(k, c, s, d, a, l, n)
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/dviread.py", line 388, in _fnt_def
tfm = _tfmfile(fontname)
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/dviread.py", line 897, in _tfmfile
return _fontfile(texname, Tfm, '.tfm', _tfmcache)
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/dviread.py", line 887, in _fontfile
filename = find_tex_file(texname + suffix)
File "/path/Python/2.7.3/lib/python2.7/site-packages/matplotlib/dviread.py", line 868, in find_tex_file
stderr=subprocess.PIPE)
File "/path/Python/2.7.3/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/path/Python/2.7.3/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
but then if I remove these matplotlib options and I remove the labels the figures are saved on the computing node without a problem.
How can I use latex for typesetting the figures on the computing node?

Latex labels with seaborn

I'm trying to plot with seaborn.kdeplot with LaTeX labels. What I tried is the following:
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(font_scale=1.5, rc={'text.usetex' : True})
x = np.linspace(-50,50,100)
y = np.sin(x)**2/x
fig = plt.figure(1)
sns.set_style('white')
sns.kdeplot(np.array(y), label='hey')
fig.gca().set(xlabel=r'$e(t_0)$ [s]', ylabel='PDF')
fig.savefig("seaborntest.png", close = True, verbose = True)
Running this I've got the following error:
Traceback (most recent call last):
File "./sns_problem.py", line 17, in <module>
fig.savefig("seaborntest.png", close = True, verbose = True)
File "/usr/lib64/python2.7/site-packages/matplotlib/figure.py", line 1363, in savefig
self.canvas.print_figure(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/backend_bases.py", line 2093, in print_figure
**kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 491, in print_png
FigureCanvasAgg.draw(self)
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 439, in draw
self.figure.draw(self.renderer)
File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 54, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/figure.py", line 999, in draw
func(*args)
File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 54, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/axes.py", line 2086, in draw
a.draw(renderer)
File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 54, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/axis.py", line 1050, in draw
renderer)
File "/usr/lib64/python2.7/site-packages/matplotlib/axis.py", line 999, in _get_tick_bboxes
extent = tick.label1.get_window_extent(renderer)
File "/usr/lib64/python2.7/site-packages/matplotlib/text.py", line 752, in get_window_extent
bbox, info = self._get_layout(self._renderer)
File "/usr/lib64/python2.7/site-packages/matplotlib/text.py", line 304, in _get_layout
ismath=False)
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 196, in get_text_width_height_descent
texmanager = self.get_texmanager()
File "/usr/lib64/python2.7/site-packages/matplotlib/backend_bases.py", line 597, in get_texmanager
self._texmanager = TexManager()
File "/usr/lib64/python2.7/site-packages/matplotlib/texmanager.py", line 137, in __init__
ff = rcParams['font.family'].lower()
AttributeError: 'list' object has no attribute 'lower'
What could be the problem? The strange thing is that I got this error even if I remove the line adding the labels. What is the proper way of using LaTeX in seaborn plot labels?
If you do not have latex install on your machine, this could be causing errors.
For Linux users try installing Latex:
sudo apt update
sudo apt-get install texlive-latex-extra texlive-fonts-recommended dvipng cm-super
If in notebook, restart the Kernel, and then the font should display using Latex.

Writing axes titles with units raised to negative powers in matplotlib with TeX and matching formatting

I'm writing a lab report for uni and have decided to use pyplot to draw a graph for it. The units for one of my axes is V^(-1), but I can't seem to get matplotlib to write that with the power in superscript (it only superscripts the minus sign) when using the code:
plt.xlabel('$1/U (10^5 V ^-1 )$')
I'd also like to match the fonts used for the axes titles with that used for the values on the axes if at all possible. I've tried putting:
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
at the beginning of my code, but that gives me the error:
File "C:\Users\Sam\Documents\Uni\Labs\pytry.py", line 92, in <module>
plt.savefig("Xrayplot.png")
File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\pyplot.py", line 561, in savefig
return fig.savefig(*args, **kwargs)
File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\figure.py", line 1421, in savefig
self.canvas.print_figure(*args, **kwargs)
File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backends\backend_wxagg.py", line 85, in print_figure
FigureCanvasAgg.print_figure(self, filename, *args, **kwargs)
File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backend_bases.py", line 2220, in print_figure
**kwargs)
File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backends\backend_agg.py", line 505, in print_png
FigureCanvasAgg.draw(self)
File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backends\backend_agg.py", line 451, in draw
self.figure.draw(self.renderer)
File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\artist.py", line 54, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\figure.py", line 1034, in draw
func(*args)
File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\artist.py", line 54, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\axes.py", line 2086, in draw
a.draw(renderer)
File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\artist.py", line 54, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\axis.py", line 1089, in draw
renderer)
File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\axis.py", line 1038, in _get_tick_bboxes
extent = tick.label1.get_window_extent(renderer)
File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\text.py", line 753, in get_window_extent
bbox, info, descent = self._get_layout(self._renderer)
File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\text.py", line 320, in _get_layout
ismath=False)
File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backends\backend_agg.py", line 205, in get_text_width_height_descent
renderer=self)
File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\texmanager.py", line 666, in get_text_width_height_descent
dvifile = self.make_dvi(tex, fontsize)
File "C:\Users\Sam\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\texmanager.py", line 413, in make_dvi
'LaTeX: \n\n' % repr(tex)) + report)
RuntimeError: LaTeX was not able to process the following string:
'lp'
Here is the full report generated by LaTeX:
Any help would be much appreciated.
For the superscript add { } around the text that should be super scripted. For the fonts i don't know the solution.
plt.xlabel('$1/U (10^5 V ^{-1} )$')

Categories

Resources