2014-11-02 31 views
15

重叠看一看下面的图表: enter image description hereMatplotlib显示的X刻度标记,尽管尽了最大努力

正是这种大图的次要情节: enter image description here

我看到两个问题吧。首先,x轴标签相互重叠(这是我的主要问题)。第二。 x轴小网格线的位置似乎有点过分。在图的左侧,它们看起来间隔适当。但在右边,他们似乎拥挤在主要网格线上......好像主要的网格线位置不是次要点的位置的合适倍数。

我的设置是我有一个名为df的数据框,它的行上有一个DatetimeIndex,而一列名为value的列包含浮点数。如有必要,我可以在要点中提供df内容的示例。这篇文章的底部有十几行df供参考。

这里产生的数字代码:

now = dt.datetime.now() 

fig, axes = plt.subplots(2, 2, figsize=(15, 8), dpi=200) 
for i, d in enumerate([360, 30, 7, 1]): 
    ax = axes.flatten()[i] 
    earlycut = now - relativedelta(days=d) 
    data = df.loc[df.index>=earlycut, :] 
    ax.plot(data.index, data['value']) 
    ax.xaxis_date() 

    ax.get_xaxis().set_minor_locator(mpl.ticker.AutoMinorLocator()) 
    ax.get_yaxis().set_minor_locator(mpl.ticker.AutoMinorLocator()) 

    ax.grid(b=True, which='major', color='w', linewidth=1.5) 
    ax.grid(b=True, which='minor', color='w', linewidth=0.75) 

什么是我在这里得到的x轴标签停止相互重叠(在四个次要情节的)最好的选择?此外,单独(但不太紧急),左上角子图中的小勾号问题有什么问题?

我在熊猫0.13.1,numpy 1.8.0和matplotlib 1.4.x.

这里是df一个小片段供参考:

        id scale tempseries_id value 
timestamp               
2014-11-02 14:45:10.302204+00:00 7564  F    1 68.0000 
2014-11-02 14:25:13.532391+00:00 7563  F    1 68.5616 
2014-11-02 14:15:12.102229+00:00 7562  F    1 68.9000 
2014-11-02 14:05:13.252371+00:00 7561  F    1 69.0116 
2014-11-02 13:55:11.792191+00:00 7560  F    1 68.7866 
2014-11-02 13:45:10.782227+00:00 7559  F    1 68.6750 
2014-11-02 13:35:10.972248+00:00 7558  F    1 68.4500 
2014-11-02 13:25:10.362213+00:00 7557  F    1 68.1116 
2014-11-02 13:15:10.822247+00:00 7556  F    1 68.2250 
2014-11-02 13:05:10.102200+00:00 7555  F    1 68.5616 
2014-11-02 12:55:10.292217+00:00 7554  F    1 69.0116 
2014-11-02 12:45:10.382226+00:00 7553  F    1 69.3500 
2014-11-02 12:35:10.642245+00:00 7552  F    1 69.2366 
2014-11-02 12:25:12.642255+00:00 7551  F    1 69.1250 
2014-11-02 12:15:11.122382+00:00 7550  F    1 68.7866 
2014-11-02 12:05:11.332224+00:00 7549  F    1 68.5616 
2014-11-02 11:55:11.662311+00:00 7548  F    1 68.2250 
2014-11-02 11:45:11.122193+00:00 7547  F    1 68.4500 
2014-11-02 11:35:11.162271+00:00 7546  F    1 68.7866 
2014-11-02 11:25:12.102211+00:00 7545  F    1 69.2366 
2014-11-02 11:15:10.422226+00:00 7544  F    1 69.4616 
2014-11-02 11:05:11.412216+00:00 7543  F    1 69.3500 
2014-11-02 10:55:10.772212+00:00 7542  F    1 69.1250 
2014-11-02 10:45:11.332220+00:00 7541  F    1 68.7866 
2014-11-02 10:35:11.332232+00:00 7540  F    1 68.5616 
2014-11-02 10:25:11.202411+00:00 7539  F    1 68.2250 
2014-11-02 10:15:11.932326+00:00 7538  F    1 68.5616 
2014-11-02 10:05:10.922229+00:00 7537  F    1 68.9000 
2014-11-02 09:55:11.602357+00:00 7536  F    1 69.3500 

编辑:试图fig.autofmt_xdate(): 我不认为这会做的伎俩。这似乎对左侧的两个图和右侧的两个图都使用相同的x-tick标签。根据我的数据,这是不正确的。请参考下面的问题输出:

enter image description here

回答

14

好了,终于搞定了。诀窍是使用plt.setp手动旋转刻度标签。使用fig.autofmt_xdate()不起作用,因为它在图中有多个子图时会出现一些意想不到的情况。下面是其产量的工作代码:

for i, d in enumerate([360, 30, 7, 1]): 
    ax = axes.flatten()[i] 
    earlycut = now - relativedelta(days=d) 
    data = df.loc[df.index>=earlycut, :] 
    ax.plot(data.index, data['value']) 

    ax.get_xaxis().set_minor_locator(mpl.ticker.AutoMinorLocator()) 
    ax.get_yaxis().set_minor_locator(mpl.ticker.AutoMinorLocator()) 

    ax.grid(b=True, which='major', color='w', linewidth=1.5) 
    ax.grid(b=True, which='minor', color='w', linewidth=0.75) 

    plt.setp(ax.get_xticklabels(), rotation=30, horizontalalignment='right') 

fig.tight_layout() 

enter image description here

顺便说一句,前面关于采取永远一些matplotlib东西的评论是非常有趣的在这里。我使用覆盆子pi作为远程位置的气象站。它正在收集数据并通过网络提供结果。男孩,哦,男孩,它真的喘息试图把这些图形。

7

由于方式文本呈现在matplotlib处理,自动检测重叠文本真的会减慢速度。 (文本占用的空间直到绘制完成后才能准确计算。)因此,matplotlib不会自动执行此操作。

因此,最好旋转长刻度标签。因为日期最常见的是这个问题,所以有一个数字方法fig.autofmt_xdate(),它将(除其他外)旋转刻度标签以使它们更具可读性。 (注意:如果您使用的是熊猫plot方法,则会返回一个轴对象,因此您需要使用ax.figure.autofmt_xdate()。)

作为一个简单的例子:

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 

time = pd.date_range('01/01/2014', '4/01/2014', freq='H') 
values = np.random.normal(0, 1, time.size).cumsum() 

fig, ax = plt.subplots() 
ax.plot_date(time, values, marker='', linestyle='-') 

fig.autofmt_xdate() 
plt.show() 

如果我们离开fig.autofmt_xdate()出来:

enter image description here

如果我们使用fig.autofmt_xdate()

enter image description here

+0

这个解决方案是否适用于几个具有不同日期时间轴的小插图(比如2x2阵列)? – 8one6 2014-11-03 03:58:22

+0

(我试图实现这个解决方案,但它只在最下面的一排子图上显示刻度标签,而那些子图不能正确对应顶层的子图)。 – 8one6 2014-11-03 04:08:59

相关问题