我有一些代码:
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)
代码生成以下图:
正如您所看到的,在中心图中,该图并未真正居中...我想将 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/