如何优化pylab x轴?

时间:2019-08-01 13:37:18

标签: python matplotlib

当我尝试绘制一个以日期为X轴的非常长的数据集时,日期会相互重叠。我想有一个清晰的x轴来自动拟合数据的比例。

import pylab

#MonthOil = \[A list of 1200 readings\]
#OilProd  = \[A list of 1200 readings\]
#MonthGas = \[A list of 1200 readings\]
#GasProd  = \[A list of 1200 readings\]

pylab.figure(1)
#Oil Production Plotting part
pylab.subplot(211)
pylab.plot(MonthOil,OilProd,'g--')
title = 'API=%7.1f' % (API)
pylab.xlabel('Time (months)')
pylab.ylabel('Oil Rate (bpm)')
pylab.grid(True)
pylab.title(title)

#Gas Production Plotting part
pylab.subplot(212)
pylab.plot(MonthGas,GasProd,'r--')
title = 'API=%7.1f' % (API)
pylab.xlabel('Time (months)')
pylab.ylabel('Gas Pro Rate (MCF)')
pylab.grid(True)
pylab.title(title)
pylab.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.95, wspace=0.35)
pylab.savefig(str(str(API)+".png"),bbox_inches='tight', pad_inches=0, transparent=False, edgecolor="none")
pylab.show()][1]

1 个答案:

答案 0 :(得分:0)

您可以做的是通过使用旋转变量修改x axis来旋转x-ticks输入。

import matplotlib.pyplot as plt


x = [1, 2, 3, 4]
y = [1, 4, 9, 6]
labels = ['Frogs', 'Hogs', 'Bogs', 'Slogs']

plt.plot(x, y)
# You can specify a rotation for the tick labels in degrees or with keywords.
plt.xticks(x, labels, rotation='vertical') # You can input an integer too.
# Pad margins so that markers don't get clipped by the axes
plt.margins(0.2)
# Tweak spacing to prevent clipping of tick-labels
plt.subplots_adjust(bottom=0.15)
plt.show()

enter image description here