自定义样式maplotlib(.mplstyle)

时间:2018-03-26 11:35:50

标签: python matplotlib plot

在构建自定义样式并将其保存在“.mplstyle”文件夹中之后,我将此mplstyle导入到新脚本中。不幸的是我遇到了一些错误。

home/local/lib/python2.7/site-packages/matplotlib/__init__.py:1102: UserWarning: Bad val "[3.7, 1.6]" on line #183
    "lines.dashed_pattern : [3.7, 1.6]
"
    in file "/home/plotting_tools/codepan.mplstyle"
    Key lines.dashed_pattern: Could not convert all entries to floats
  (val, error_details, msg))
/home/local/lib/python2.7/site-packages/matplotlib/__init__.py:1102: UserWarning: Bad val "[1.0, 1.65]" on line #184
    "lines.dotted_pattern : [1.0, 1.65]
"
    in file "/home/plotting_tools/codepan.mplstyle"
    Key lines.dotted_pattern: Could not convert all entries to floats
  (val, error_details, msg))

AttributeErrorTraceback (most recent call last)
/home/local/lib/python2.7/site-packages/IPython/core/formatters.pyc in __call__(self, obj)
    332                 pass
    333             else:
--> 334                 return printer(obj)
    335             # Finally look for special method names
    336             method = get_real_method(obj, self.print_method)

/home/local/lib/python2.7/site-packages/IPython/core/pylabtools.pyc in <lambda>(fig)
    239 
    240     if 'png' in formats:
--> 241         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
    242     if 'retina' in formats or 'png2x' in formats:
    243         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

/home/local/lib/python2.7/site-packages/IPython/core/pylabtools.pyc in print_figure(fig, fmt, bbox_inches, **kwargs)
    123 
    124     bytes_io = BytesIO()
--> 125     fig.canvas.print_figure(bytes_io, **kw)
    126     data = bytes_io.getvalue()
    127     if fmt == 'svg':

/home/local/lib/python2.7/site-packages/matplotlib/backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2210                     orientation=orientation,
   2211                     dryrun=True,
-> 2212                     **kwargs)
   2213                 renderer = self.figure._cachedRenderer
   2214                 bbox_inches = self.figure.get_tightbbox(renderer)

/home/local/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in print_png(self, filename_or_obj, *args, **kwargs)
    511 
    512     def print_png(self, filename_or_obj, *args, **kwargs):
--> 513         FigureCanvasAgg.draw(self)
    514         renderer = self.get_renderer()
    515         original_dpi = renderer.dpi

/home/local/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in draw(self)
    431             # if toolbar:
    432             #     toolbar.set_cursor(cursors.WAIT)
--> 433             self.figure.draw(self.renderer)
    434             # A GUI class may be need to update a window using this draw, so
    435             # don't forget to call the superclass.

/home/local/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args, **kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

/home/local/lib/python2.7/site-packages/matplotlib/figure.pyc in draw(self, renderer)
   1470 
   1471             if self.frameon:
-> 1472                 self.patch.draw(renderer)
   1473 
   1474             mimage._draw_list_compositing_images(

/home/local/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args, **kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

/home/local/lib/python2.7/site-packages/matplotlib/patches.pyc in draw(self, renderer)
    556             renderer = PathEffectRenderer(self.get_path_effects(), renderer)
    557 
--> 558         renderer.draw_path(gc, tpath, affine, rgbFace)
    559 
    560         gc.restore()

/home/local/lib/python2.7/site-packages/matplotlib/patheffects.pyc in draw_path(self, gc, tpath, affine, rgbFace)
    107     def draw_path(self, gc, tpath, affine, rgbFace=None):
    108         for path_effect in self._path_effects:
--> 109             path_effect.draw_path(self._renderer, gc, tpath, affine,
    110                                   rgbFace)
    111 

AttributeError: 'unicode' object has no attribute 'draw_path'<Figure size 432x288 with 1 Axes>

我怀疑这与我如何格式化“.mplstyle”文件有关,但我不太确定。如果您需要有关我的计算机或我正在运行的脚本的更多信息,请告诉我。

修改

这是我使用的代码

# Example of Use
import matplotlib.pyplot as plt
#plt.style.use("codepan.mplstyle")
plt.style.use('ggplot')

n = 50
raw_data = np.random.randn(n)*10

plt.plot(raw_data)
plt.show()

这是mplstyle文件的片段

lines.dashdot_pattern : [6.4, 1.6, 1.0, 1.6]
lines.dashed_pattern : [3.7, 1.6]
lines.dotted_pattern : [1.0, 1.65]

1 个答案:

答案 0 :(得分:1)

您想要从这些行中删除方括号

即。他们应该看起来像:

lines.dashdot_pattern : 6.4, 1.6, 1.0, 1.6
lines.dashed_pattern : 3.7, 1.6
lines.dotted_pattern : 1.0, 1.65

例如,请参阅classic.mplstyle示例表中的here