Closed
Description
First of all, thanks for working on MPL -- it's an indispensable package for me.
Bug report
I have a figure that I try to export to SVG, and it fails with "Cannot cast array data from dtype('<U7') to dtype('float64') according to the rule 'safe'"
Code for reproduction
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# Ensure clean export of text
plt.rc('pdf',fonttype=42)
plt.rc('ps',fonttype=42)
plt.rc('svg',fonttype='none')
fig, ax = plt.subplots(figsize=(12, 8))
idx = pd.date_range("1972-01-01", "2017-05-01", freq="1M")
ser = pd.Series(np.random.normal(size=len(idx)), idx)
ser.plot(ax=ax, label="A random time series")
ax.xaxis.set_label_text("")
ax.yaxis.set_label_text("Bond Yield (%)")
start = '1973-11'
ypos = ser.loc[start]
ax.annotate('Geopolitical event 1',
xy=(start, ypos),
xytext=(start, 2.5),
arrowprops=dict(arrowstyle="->", color='k'))
plt.savefig("fun.svg")
Actual outcome
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-51e1c1b21bd7> in <module>
34 # ax.axvspan(start, end, facecolor='g', alpha=0.2, label="A very significant event")
35 # grrr -- export to svg does not work on this file! Export to PDF and convert to svg in Inkscape
---> 36 plt.savefig("fun.svg")
~/Applications/miniconda3/envs/jl10/lib/python3.7/site-packages/matplotlib/pyplot.py in savefig(*args, **kwargs)
720 def savefig(*args, **kwargs):
721 fig = gcf()
--> 722 res = fig.savefig(*args, **kwargs)
723 fig.canvas.draw_idle() # need this if 'transparent=True' to reset colors
724 return res
~/Applications/miniconda3/envs/jl10/lib/python3.7/site-packages/matplotlib/figure.py in savefig(self, fname, transparent, **kwargs)
2178 self.patch.set_visible(frameon)
2179
-> 2180 self.canvas.print_figure(fname, **kwargs)
2181
2182 if frameon:
~/Applications/miniconda3/envs/jl10/lib/python3.7/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, **kwargs)
2080 orientation=orientation,
2081 bbox_inches_restore=_bbox_inches_restore,
-> 2082 **kwargs)
2083 finally:
2084 if bbox_inches and restore_bbox:
~/Applications/miniconda3/envs/jl10/lib/python3.7/site-packages/matplotlib/backends/backend_svg.py in print_svg(self, filename, *args, **kwargs)
1200 detach = True
1201
-> 1202 result = self._print_svg(filename, fh, **kwargs)
1203
1204 # Detach underlying stream from wrapper so that it remains open in
~/Applications/miniconda3/envs/jl10/lib/python3.7/site-packages/matplotlib/backends/backend_svg.py in _print_svg(self, filename, fh, dpi, bbox_inches_restore, **kwargs)
1225 bbox_inches_restore=bbox_inches_restore)
1226
-> 1227 self.figure.draw(renderer)
1228 renderer.finalize()
1229
~/Applications/miniconda3/envs/jl10/lib/python3.7/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
36 renderer.start_filter()
37
---> 38 return draw(artist, renderer, *args, **kwargs)
39 finally:
40 if artist.get_agg_filter() is not None:
~/Applications/miniconda3/envs/jl10/lib/python3.7/site-packages/matplotlib/figure.py in draw(self, renderer)
1707 self.patch.draw(renderer)
1708 mimage._draw_list_compositing_images(
-> 1709 renderer, self, artists, self.suppressComposite)
1710
1711 renderer.close_group('figure')
~/Applications/miniconda3/envs/jl10/lib/python3.7/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
133 if not_composite or not has_images:
134 for a in artists:
--> 135 a.draw(renderer)
136 else:
137 # Composite any adjacent images together
~/Applications/miniconda3/envs/jl10/lib/python3.7/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
36 renderer.start_filter()
37
---> 38 return draw(artist, renderer, *args, **kwargs)
39 finally:
40 if artist.get_agg_filter() is not None:
~/Applications/miniconda3/envs/jl10/lib/python3.7/site-packages/matplotlib/axes/_base.py in draw(self, renderer, inframe)
2645 renderer.stop_rasterizing()
2646
-> 2647 mimage._draw_list_compositing_images(renderer, self, artists)
2648
2649 renderer.close_group('axes')
~/Applications/miniconda3/envs/jl10/lib/python3.7/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
133 if not_composite or not has_images:
134 for a in artists:
--> 135 a.draw(renderer)
136 else:
137 # Composite any adjacent images together
~/Applications/miniconda3/envs/jl10/lib/python3.7/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
36 renderer.start_filter()
37
---> 38 return draw(artist, renderer, *args, **kwargs)
39 finally:
40 if artist.get_agg_filter() is not None:
~/Applications/miniconda3/envs/jl10/lib/python3.7/site-packages/matplotlib/text.py in draw(self, renderer)
2367 # Draw text, including FancyBboxPatch, after FancyArrowPatch.
2368 # Otherwise, a wedge arrowstyle can land partly on top of the Bbox.
-> 2369 Text.draw(self, renderer)
2370
2371 def get_window_extent(self, renderer=None):
~/Applications/miniconda3/envs/jl10/lib/python3.7/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
36 renderer.start_filter()
37
---> 38 return draw(artist, renderer, *args, **kwargs)
39 finally:
40 if artist.get_agg_filter() is not None:
~/Applications/miniconda3/envs/jl10/lib/python3.7/site-packages/matplotlib/text.py in draw(self, renderer)
716 textrenderer.draw_text(gc, x, y, clean_line,
717 textobj._fontproperties, angle,
--> 718 ismath=ismath, mtext=mtext)
719
720 gc.restore()
~/Applications/miniconda3/envs/jl10/lib/python3.7/site-packages/matplotlib/backends/backend_svg.py in draw_text(self, gc, x, y, s, prop, angle, ismath, mtext)
1160 self._draw_text_as_path(gc, x, y, s, prop, angle, ismath, mtext)
1161 else:
-> 1162 self._draw_text_as_text(gc, x, y, s, prop, angle, ismath, mtext)
1163
1164 if gc.get_url() is not None:
~/Applications/miniconda3/envs/jl10/lib/python3.7/site-packages/matplotlib/backends/backend_svg.py in _draw_text_as_text(self, gc, x, y, s, prop, angle, ismath, mtext)
1044 # Get anchor coordinates.
1045 transform = mtext.get_transform()
-> 1046 ax, ay = transform.transform_point(mtext.get_position())
1047 ay = self.height - ay
1048
~/Applications/miniconda3/envs/jl10/lib/python3.7/site-packages/matplotlib/transforms.py in transform_point(self, point)
1479 if len(point) != self.input_dims:
1480 raise ValueError("The length of 'point' must be 'self.input_dims'")
-> 1481 return self.transform(np.asarray([point]))[0]
1482
1483 def transform_path(self, path):
~/Applications/miniconda3/envs/jl10/lib/python3.7/site-packages/matplotlib/transforms.py in transform(self, values)
1392
1393 # Transform the values
-> 1394 res = self.transform_affine(self.transform_non_affine(values))
1395
1396 # Convert the result back to the shape of the input values.
~/Applications/miniconda3/envs/jl10/lib/python3.7/site-packages/matplotlib/transforms.py in transform_affine(self, points)
2371 def transform_affine(self, points):
2372 # docstring inherited
-> 2373 return self.get_affine().transform(points)
2374
2375 def transform_non_affine(self, points):
~/Applications/miniconda3/envs/jl10/lib/python3.7/site-packages/matplotlib/transforms.py in transform(self, values)
1696 def transform(self, values):
1697 # docstring inherited
-> 1698 return self.transform_affine(values)
1699
1700 def transform_affine(self, values):
~/Applications/miniconda3/envs/jl10/lib/python3.7/site-packages/matplotlib/transforms.py in transform_affine(self, points)
1779 tpoints = affine_transform(points.data, mtx)
1780 return np.ma.MaskedArray(tpoints, mask=np.ma.getmask(points))
-> 1781 return affine_transform(points, mtx)
1782
1783 def transform_point(self, point):
TypeError: Cannot cast array data from dtype('<U7') to dtype('float64') according to the rule 'safe'
Expected outcome
I would have expected the SVG to be written. Writing to PDF works.
Matplotlib version
- Operating system: macOS 10.14.5
- Matplotlib version: 3.1.1
- Matplotlib backend (
print(matplotlib.get_backend())
): 'svg', 'agg', module://ipykernel.pylab.backend_inline - Python version: 3.7.3
- Jupyter version (if applicable): 4.4.0
- Other libraries: pandas, numpy
MPL installed via pip.
Thanks!