首页 > 解决方案 > Pyplot-扩展x轴

问题描述

这是我用来产生以下输出的代码:

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

    xedges = [27.5, 35, 42.5, 50, 57.5, 65, 72.5, 80, 87.5, 95, 102.5, 110, 117.5, 125, 132.5, 140, 147.5, 155, 162.5]
    yedges = [0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300, 320, 340, 360, 380]
    H = pd.read_stata ('R:/data.dta')
    fig = plt.figure()
    plt.imshow(H, cmap="nipy_spectral", interpolation='bilinear', origin='low',extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]])

在此处输入图像描述

图上的 x 轴看起来非常压缩。我希望能够生成相同的图表,但 x 轴更宽,我该怎么做?

我怀疑它与“范围”参数有关,但我不确定该怎么做。

标签: pythonmatplotlibgraph

解决方案


我刚刚想通了。解决方案是在代码中将纵横比设置为“自动”,如下所示:

plt.imshow(H, cmap="nipy_spectral", interpolation='bilinear', origin='low',aspect = 'auto', extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]])

推荐阅读