python - 使用 matplotlib/python 设置绘图轴限制时出现问题

标签 python matplotlib

我有一些代码:

def print_fractures(fractures):
    # generate ends for the line segments
    xpairs = []
    ypairs = []
    plt.subplot(132)

    for i in range(len(fractures)):
        xends = [fractures[i][1][0], fractures[i][2][0]]
        yends = [fractures[i][1][1], fractures[i][2][1]]
        xpairs.append(xends)
        ypairs.append(yends)
    for xends,yends in zip(xpairs,ypairs):
        plt.plot(xends, yends, 'b-', alpha=0.4)
        plt.plot()
    plt.xlabel("X Coordinates (m)")
    plt.ylabel("Y Coordinates (m)")
    plt.ylim((0,400))


def histogram(spacings):
    plt.subplot(131)
    plt.hist(np.vstack(spacings), bins = range(0,60,3), normed=True)
    plt.xlabel('Spacing (m)')
    plt.ylabel('Frequency (count)')
    #plt.title("Fracture Spacing Histogram")


def plotCI(sample, ciHigh, ciLow, avgInts):
    plt.subplot(133)    
    plt.plot(sample,ciHigh)
    plt.plot(sample,avgInts)
    plt.plot(sample,ciLow)
    plt.legend(['Mean + 95% CI', 'Mean', 'Mean - 95% CI'])
    plt.title("Intersections vs Number of Wells")
    plt.xlabel("# of Wells")
    plt.ylabel("# of Intersections for " + str(bedT) + "m bed thickness")

def makeplots(spacings,fractures): 
    histogram(spacings)
    print_fractures(fractures)
    plt.axis('equal')
    plotCI(sample, ciHigh, ciLow, avgInts)
    plt.show()
makeplots(spacings,fractures)

代码生成以下图: enter image description here

正如您所看到的,在中心图中,该图并未真正居中...我想将 x 轴和 y 轴设置为 (0,400),但我遇到了麻烦。

到目前为止我已经尝试过:

plt.axis((0,400,0,400))

和:

plt.ylim((0,400))
plt.xlim((0,400))

这两个选项都不适合我。

最佳答案

在您的示例中,没有显示如何/何时调用函数。对于 plt.ion()/plt.ioff() 以及执行 xlim() 时实际绘图的焦点,可能存在一些副作用/ylim() 命令。

为了完全控制(特别是当有多个绘图时),通常最好有明确的图形和绘图句柄,例如:

fg = plt.figure(1)
fg.clf()  # clear figure, just in case the script is not executed for the first time 
ax1 = fg.add_subplot(2,1,1)
ax1.plot([1,2,3,4], [10,2,5,0])
ax1.set_ylim((0,5)) # limit yrange

ax2 = fg.add_subplot(2,1,2)    
...

fg.canvas.draw()  # the figure is drawn at this point

plt.show()  # enter GUI event loop, needed in non-interactive interpreters

关于python - 使用 matplotlib/python 设置绘图轴限制时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21488789/

相关文章:

python - 列上的十分位数 Pandas DataFrame

python - matplotlib 中的中心线断轴标签

python - 如何绘制 pandas 系列的实时图表?并间歇性地从文件中读取

Python Matplotlib slider 小部件未更新

python - 如何在matplotlib中从下到上垂直制作渐变颜色

python - 将 pandas 数据框转换为嵌套字典

没有索引和项目的Python for循环

python - 将数组拆分为不均匀的组

python - 将 matplotlib png 转换为 base64 以便在 html 模板中查看

python - 如何使用 seaborn factorplot 更改图形大小