Matplotlib 轴标签将科学指数移动到同一行 [英] Matplotlib axis label move scientific exponent into same line

查看:58
本文介绍了Matplotlib 轴标签将科学指数移动到同一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在制作一个图,其在 x 轴上的范围从 0 到 1.3e7.我将其绘制如下:

plt.errorbar(num_vertices,sampled_ave_path_average,yerr=sampled_ave_path_stdev,fmt='.',markersize='1',capsize=2,capthick=2)plt.xlabel('顶点数')plt.ylabel('平均最短路径长度')plt.xlim(0,1.3*10**7)plt.savefig('path_length_vs_N.eps', bbox_inches='tight')plt.savefig('path_length_vs_N.png', bbox_inches='tight')plt.close()

这会生成一个图表,其中 x 轴刻度标签采用科学记数法,这正是我想要的.然而,我想知道是否可以将 1e7(下面用红色圈出)移动到与其他标签相同的行上?(我意识到这可能会导致对其他值的指数的混淆.)

解决方案

首先,您可以看看以下问题:

  • I am currently producing a plot which on the x-axis ranges from 0 to 1.3e7. I am plotting this as follows:

    plt.errorbar(num_vertices,sampled_ave_path_average,yerr=sampled_ave_path_stdev,fmt='.',markersize='1',capsize=2,capthick=2)
    plt.xlabel('Number of vertices')
    plt.ylabel('Average shortest path length')
    plt.xlim(0,1.3*10**7)
    plt.savefig('path_length_vs_N.eps', bbox_inches='tight')
    plt.savefig('path_length_vs_N.png', bbox_inches='tight')
    plt.close()
    

    This produces a plot where the x-axis tick labels are in scientific notation which is what I would like. I was however wondering whether it is possible to move the 1e7 (circled in red below) onto the same line as the other labels? (I realise this could cause confusion about the exponents of the other values.)

    解决方案

    First, you may look at the following questions:

    The first one may be a possible alternative (because you mention that the envisioned solution "might cause confusion about the exponents"). The second might point a way to a possible solution although it is about using a colorbar.

    So in order to change the position of the offset text to be in line with the xtick labels, the following would be a way to go.

    import matplotlib.pyplot as plt
    import numpy as np
    import types
    
    x = np.linspace(1e7, 9e7)
    y = 1-np.exp(-np.linspace(0,5))
    fig, ax = plt.subplots()
    ax.plot(x,y)
    
    
    pad = plt.rcParams["xtick.major.size"] + plt.rcParams["xtick.major.pad"]
    def bottom_offset(self, bboxes, bboxes2):
        bottom = self.axes.bbox.ymin
        self.offsetText.set(va="top", ha="left") 
        oy = bottom - pad * self.figure.dpi / 72.0
        self.offsetText.set_position((1, oy))
    
    ax.xaxis._update_offset_text_position = types.MethodType(bottom_offset, ax.xaxis)
    
    plt.show()
    

    这篇关于Matplotlib 轴标签将科学指数移动到同一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆