- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 set_major_formatter 格式化 matplotlib 图表上的 y 轴。绘图正确生成,但 ax.yaxis.set_major_formatter() 抛出一些奇怪的错误。
格式化程序:
def mjrFormatter(x):
return "{0:.0f}%".format(x * 100)
使用格式化程序的代码:
...
ax.yaxis.set_major_formatter(mjrFormatter)
...
错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-108-b436fe657a8b> in <module>()
----> 1 plot_func(data = data, figsize=(20,10), fig_title = 'title')
<ipython-input-107-d60ffc010a75> in plot_percent_moc(data, figsize, fig_title)
16 _ = data2[col].plot()
17
---> 18 ax.yaxis.set_major_formatter(mjrFormatter)
19
20 fig.suptitle(fig_title, fontsize = 14)
C:\Python27\lib\site-packages\matplotlib\axis.pyc in set_major_formatter(self, formatter)
1396 self.isDefault_majfmt = False
1397 self.major.formatter = formatter
-> 1398 formatter.set_axis(self)
1399
1400 def set_minor_formatter(self, formatter):
AttributeError: 'function' object has no attribute 'set_axis'
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
C:\Python27\lib\site-packages\IPython\zmq\pylab\backend_inline.pyc in show(close)
100 try:
101 for figure_manager in Gcf.get_all_fig_managers():
--> 102 send_figure(figure_manager.canvas.figure)
103 finally:
104 show._to_draw = []
C:\Python27\lib\site-packages\IPython\zmq\pylab\backend_inline.pyc in send_figure(fig)
209 """
210 fmt = InlineBackend.instance().figure_format
--> 211 data = print_figure(fig, fmt)
212 # print_figure will return None if there's nothing to draw:
213 if data is None:
C:\Python27\lib\site-packages\IPython\core\pylabtools.pyc in print_figure(fig, fmt)
102 try:
103 bytes_io = BytesIO()
--> 104 fig.canvas.print_figure(bytes_io, format=fmt, bbox_inches='tight')
105 data = bytes_io.getvalue()
106 finally:
C:\Python27\lib\site-packages\matplotlib\backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
2050 orientation=orientation,
2051 dryrun=True,
-> 2052 **kwargs)
2053 renderer = self.figure._cachedRenderer
2054 bbox_inches = self.figure.get_tightbbox(renderer)
C:\Python27\lib\site-packages\matplotlib\backends\backend_agg.pyc in print_png(self, filename_or_obj, *args, **kwargs)
489
490 def print_png(self, filename_or_obj, *args, **kwargs):
--> 491 FigureCanvasAgg.draw(self)
492 renderer = self.get_renderer()
493 original_dpi = renderer.dpi
C:\Python27\lib\site-packages\matplotlib\backends\backend_agg.pyc in draw(self)
437
438 try:
--> 439 self.figure.draw(self.renderer)
440 finally:
441 RendererAgg.lock.release()
C:\Python27\lib\site-packages\matplotlib\artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
52 def draw_wrapper(artist, renderer, *args, **kwargs):
53 before(artist, renderer)
---> 54 draw(artist, renderer, *args, **kwargs)
55 after(artist, renderer)
56
C:\Python27\lib\site-packages\matplotlib\figure.pyc in draw(self, renderer)
997 dsu.sort(key=itemgetter(0))
998 for zorder, a, func, args in dsu:
--> 999 func(*args)
1000
1001 renderer.close_group('figure')
C:\Python27\lib\site-packages\matplotlib\artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
52 def draw_wrapper(artist, renderer, *args, **kwargs):
53 before(artist, renderer)
---> 54 draw(artist, renderer, *args, **kwargs)
55 after(artist, renderer)
56
C:\Python27\lib\site-packages\matplotlib\axes.pyc in draw(self, renderer, inframe)
2084
2085 for zorder, a in dsu:
-> 2086 a.draw(renderer)
2087
2088 renderer.close_group('axes')
C:\Python27\lib\site-packages\matplotlib\artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
52 def draw_wrapper(artist, renderer, *args, **kwargs):
53 before(artist, renderer)
---> 54 draw(artist, renderer, *args, **kwargs)
55 after(artist, renderer)
56
C:\Python27\lib\site-packages\matplotlib\axis.pyc in draw(self, renderer, *args, **kwargs)
1046 renderer.open_group(__name__)
1047
-> 1048 ticks_to_draw = self._update_ticks(renderer)
1049 ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw,
1050 renderer)
C:\Python27\lib\site-packages\matplotlib\axis.pyc in _update_ticks(self, renderer)
933
934 interval = self.get_view_interval()
--> 935 tick_tups = [t for t in self.iter_ticks()]
936 if self._smart_bounds:
937 # handle inverted limits
C:\Python27\lib\site-packages\matplotlib\axis.pyc in iter_ticks(self)
879 majorLocs = self.major.locator()
880 majorTicks = self.get_major_ticks(len(majorLocs))
--> 881 self.major.formatter.set_locs(majorLocs)
882 majorLabels = [self.major.formatter(val, i)
883 for i, val in enumerate(majorLocs)]
AttributeError: 'function' object has no attribute 'set_locs'
我做错了什么还是这是一个错误?
最佳答案
该错误是因为 set_major_formatter
假定它正在获取 Formatter
对象 ( doc ),因此当它尝试对您的函数执行操作时,它会崩溃。
我认为您正在寻找 FormatStrFormatter
或 FuncFormatter
。 (doc)
def mjrFormatter(x, pos):
return "{0:.0f}%".format(x * 100)
gca().yaxis.set_major_formatter(mpl.ticker.FuncFormatter(mjrFormatter))
签名中的第二个参数是强制性的。
或(它不做你想要的,但密切相关)
gca().yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2f%%'))
关于Matplotlib set_major_formatter 属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14731879/
我正在尝试使用 set_major_formatter 格式化 matplotlib 图表上的 y 轴。绘图正确生成,但 ax.yaxis.set_major_formatter() 抛出一些奇怪的错
我有以下代码: import pandas as pd from pandas import DataFrame as df import matplotlib from pandas_datarea
我是 matplotlib 的新手。我从 here 复制了简单 pyqt-matplotlib 示例的代码,并更改了 class PlotCanvas 的 plot() 以制作值(value)时间图表
我有一个 Pandas 数据框,我正在尝试绘制两列的对比图。但问题是我的 xtick 标签显示得很精确。如何缩写(我的 X 轴值是元组)? fig = plt.figure(figsize=(13,1
我是一名优秀的程序员,十分优秀!