使用 matplotlib 格式化子图中的轴标签

时间:2021-01-15 15:09:38

标签: python matplotlib plot axis-labels

所以,我在使用 matplotlib 格式化轴标签时遇到了两个问题。 我制作了一些更改轴标签的 x-y 数据的子图。实际上,这些数据是我用来进行线性拟合的 unix 时间戳浮点数。然后我以一种更易读的方式将它们格式化,并使用时间和日期时间作为绘图的标签。到目前为止,我成功了,但是...我有两个问题:

  • 首先,标签(在 x 和 y 轴上)是重叠的
  • 然后在 x 轴上,标签似乎向右移动而不是匹配点的线(即使它们旋转)

这是情节的视图:

![enter image description here

我的代码如下:

def fit_one(x,y,lx,ly):
    par = np.polyfit(x, y, 1)
    q,m = np.polyfit(x, y, 1, cov=True)
    fit_fn=np.poly1d(par)
    fig, ax = plt.subplots(figsize=(15,15))
    
    ax.set_xticks(x)
    ax.set_xticklabels(lx, rotation=60)
    ax.set_yticks(y)
    ax.set_yticklabels(ly)
    ax.set_ylabel('gtu_time', fontsize = 12)
    ax.set_xlabel('entry_time',fontsize = 12)
    ax.plot(x,y,'k*',label='data')
    ax.plot(x,fit_fn(x),color='red', label='fit')
    #fig.tight_layout()

    fig.savefig('data_fit.pdf')
    #plt.xlim((1571512436,1609826672))
    plt.show()
    chi_squared = np.sum((np.polyval(par, x) - y) ** 2)
    print '\n\n'
    print 'Chi2   =    %.3f' % chi_squared  
    print 'p0     =    %.9f     +/- %.9f' % (q[1], np.sqrt(m[1][1]))
    print 'p1     =    %.9f     +/- %.9f' % (q[0], np.sqrt(m[0][0]))
    print '\n\n', par

此外,我想了解为什么如果我在 loc='right' 行添加 ax.set_xlabel('entry_time',fontsize = 12) 会出现以下错误:

<块引用>

属性错误:未知属性位置

当它在 matplotlib 文档中时,我真的不明白。

对如何解决这些问题有什么建议吗? 谢谢!

0 个答案:

没有答案
相关问题