Matplotlib - 扩展MaxNLocator(或日期定位器)

时间:2015-07-27 20:49:49

标签: python matplotlib

是否有办法同时指定主刻度线的位置并自动将其余部分填充到某个数字,例如 (lldb) bt * thread #1: tid = 0x1d9f18, 0x00000001002d5bac libglInterpose.dylib`EAGLContext_renderbufferStorageFromDrawable(EAGLContext*, objc_selector*, unsigned long, id<EAGLDrawable>) + 180, queue = 'com.apple.spritekit.renderQueue', stop reason = EXC_BAD_ACCESS (code=1, address=0x11f0) frame #0: 0x00000001002d5bac libglInterpose.dylib`EAGLContext_renderbufferStorageFromDrawable(EAGLContext*, objc_selector*, unsigned long, id<EAGLDrawable>) + 180 frame #1: 0x0000000189252468 SpriteKit`-[SKView remakeFramebuffer:] + 600 frame #2: 0x0000000189252e44 SpriteKit`-[SKView _setupContext] + 832 frame #3: 0x0000000189253700 SpriteKit`-[SKView _renderContent] + 112 frame #4: 0x00000001008a4f94 libdispatch.dylib`_dispatch_client_callout + 16 frame #5: 0x00000001008af08c libdispatch.dylib`_dispatch_barrier_sync_f_invoke + 128 frame #6: 0x00000001892535a0 SpriteKit`-[SKView renderContent] + 100 frame #7: 0x0000000189254d24 SpriteKit`-[SKView layoutSubviews] + 564 frame #8: 0x00000001893dd760 UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 580 frame #9: 0x0000000188d25e1c QuartzCore`-[CALayer layoutSublayers] + 152 frame #10: 0x0000000188d20884 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 320 frame #11: 0x0000000188d20728 QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 32 frame #12: 0x0000000188d1febc QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 276 frame #13: 0x0000000188d1fc3c QuartzCore`CA::Transaction::commit() + 528 frame #14: 0x000000018966c838 UIKit`-[UIApplication _reportMainSceneUpdateFinished:] + 60 frame #15: 0x000000018966d71c UIKit`-[UIApplication _runWithMainScene:transitionContext:completion:] + 2804 frame #16: 0x000000018966b778 UIKit`-[UIApplication workspaceDidEndTransaction:] + 184 frame #17: 0x000000018d1a93c8 FrontBoardServices`__31-[FBSSerialQueue performAsync:]_block_invoke_2 + 32 frame #18: 0x000000018495827c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 20 frame #19: 0x0000000184957384 CoreFoundation`__CFRunLoopDoBlocks + 312 frame #20: 0x00000001849559a8 CoreFoundation`__CFRunLoopRun + 1756 frame #21: 0x00000001848812d4 CoreFoundation`CFRunLoopRunSpecific + 396 frame #22: 0x000000018944c43c UIKit`-[UIApplication _run] + 552 frame #23: 0x0000000189446fac UIKit`UIApplicationMain + 1488 * frame #24: 0x0000000100046804 Ascii Attack`main + 164 at AppDelegate.swift:11 frame #25: 0x000000019684aa08 libdyld.dylib`start + 4 (lldb)

EG。我有2010年至今的数据。我想指定我想要10个刻度。如果我首先指定我希望年份位于MaxNLocator,那么这将找到5个滴答(2011,2012,2011,2013,2014,2015 - 从1月开始)并且剩下5个滴答。我可以自动选择这些,然后使用matplotlib.dates.YearLocator()的功能吗?

目前我为MaxNLocator()定义了我自己的方法:

FuncFormatter

这几乎到了那里。这将显示第一次刻度与年份的年份变化,否则它只显示月份。它的进步,但不太正确。具有一年的月份将是year = 0 major_ticks = [] def formatter(x, pos): x = matplotlib.dates.num2date(x) print x print pos global year global major_ticks if x.year > year: fmt = '%B \n %Y' label = x.strftime(fmt) major_ticks.append(pos) else: fmt = '%B' label = x.strftime(fmt) year = x.year return label loc = MaxNLocator(nbins=8) fmt = FuncFormatter(formatter) fig, ax = plt.subplots() plt.plot(df.index, df['col1']) plt.show() 选择的首次显示的月份,其中我想指定显示所有1月份并显示其年份。

enter image description here

这是我上面代码的输出 - 我想强制1月份显示并均匀分配我的刻度。谢谢你的帮助。 编辑:我还没有取得任何进展 - 任何想法?

0 个答案:

没有答案