【问题标题】:place labels between ticks在刻度之间放置标签
【发布时间】:2010-12-09 21:45:36
【问题描述】:

在 matplotlib 中,如何在刻度之间放置刻度标签(不在刻度下方)

例如:当绘制股票价格随时间的变化时,我希望 x 轴次要刻度显示月份和年份,以显示在连续的 x 轴主要刻度之间(不仅仅是在主要刻度下方)

---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---
  jan feb mar apr may jun jul aug sep oct nov dec jan feb mar apr may jun jul aug sep
                       2008                                            2009

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    这能解决问题吗?

    enter code here
    x = 'j f m a m j j a s o n d j f m a m j j a s o n d'.split()
    y = abs(randn(24))
    x[6] = 'j\n2008' # replace "j" (January) with ('j' and the appropriate year
    x[18] = 'j\n2009'
    bar(xrange(len(x)), y, width=0.1)
    bar(xrange(len(x)), y, width=0.1)
    xticks(xrange(len(x)), x, ha='center')
    

    【讨论】:

    • 图片不可用(imageshack 主页)
    • @PierreCordier 谢谢,已修复
    【解决方案2】:

    你的意思是这样的: - http://matplotlib.sourceforge.net/examples/pylab_examples/barchart_demo.html ??

    您必须使用 xticks 和 ha(或“水平对齐”)参数:

    >>> x = 'j f m a m j j a s o n d'.split()
    >>> y = abs(randn(12))
    >>> bar(xrange(len(x)), y, width=0.1)
    
    >>> xticks(xrange(len(x)), x, ha='center')
    

    查看 help(xticks) 和 help(matplotlib.text.Text) 以获得更多选项

    编辑:抱歉,我没有看到您还询问如何将年份标签放在刻度下方。我认为您必须手动执行此操作,请查看我链接的示例以了解如何执行此操作。

    【讨论】:

      猜你喜欢
      • 2017-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-03
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多