制作包括条形图的子图

时间:2016-02-03 12:33:12

标签: python bar-chart subplot

我想绘制3个子图,第一个图是条形图。当我这样做时,x轴向左移动。我看起来非常奇怪的条形图,见下图。如果我改变了''''通过'' plot''它工作得很好,但图表是一条线而不是条形。

enter image description here

kwargs=dict(delimiter='\t',\
        skip_header=0,\
        missing_values='NaN',\
        converters={0:matplotlib.dates.strpdate2num('%d-%m-%Y %H:%M')},\
        dtype = float,\
        names=True,\
        )

storm_dis_cats = np.genfromtxt('C:\Users\ker\Documents\Discharge_data_Catsop_27-06-2014.txt',**kwargs)

kwargs=dict(delimiter='\t',\
        skip_header=0,\
        missing_values='NaN',\
        converters={0:matplotlib.dates.strpdate2num('%d-%m-%Y %H:%M')},\
        dtype = float,\
        names=True,\
        )

storm_stage_cats = np.genfromtxt('C:\Users\ker\Documents\Stageheight_data_Catsop_27-06-2014.txt',**kwargs)

kwargs=dict(delimiter='\t',\
        skip_header=0,\
        missing_values='NaN',\
        converters={0:matplotlib.dates.strpdate2num('%d-%m-%Y %H:%M')},\
        dtype = float,\
        names=True,\
        )

storm_rain_cats = np.genfromtxt('C:\Users\ker\Documents\Rainfall_data_Catsop_27-06-2014.txt',**kwargs)

discharge = storm_dis_cats['discharge']
date = storm_stage_cats['date']
stage = storm_stage_cats['stage'] - 79.331
date_stage = storm_stage_cats['date']
rainfall = storm_rain_cats['rainfall']
date_rainfall = storm_rain_cats['date']

#set locator and formatter

hours    = matplotlib.dates.HourLocator()   # every year
months   = matplotlib.dates.MonthLocator()  # every month
hoursFmt = matplotlib.dates.DateFormatter('%H')

#create plot

fig=matplotlib.pyplot.figure()
ax=fig.add_subplot(311)
ax.bar(date_rainfall,rainfall,color='blue',label='Precipitation')
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(hoursFmt)
matplotlib.pyplot.ylabel('Precipitation[mm]')
matplotlib.pyplot.grid(True)
matplotlib.pyplot.legend()

ax=fig.add_subplot(312)
ax.plot(date,discharge,color='red',label='discharge')
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(hoursFmt)
matplotlib.pyplot.ylabel('discharge[m3/s]')
matplotlib.pyplot.grid(True)
matplotlib.pyplot.legend()

ax=fig.add_subplot(313)
ax.plot(date_stage,stage,color='green',label='stageheight')
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(hoursFmt)
matplotlib.pyplot.ylabel('stageheight[m]')
matplotlib.pyplot.grid(True)
matplotlib.pyplot.legend()
matplotlib.pyplot.xlabel('Hours')

fig=matplotlib.pyplot.figure(1)
fig.autofmt_xdate() #rotates dates in xaxis

1 个答案:

答案 0 :(得分:0)

我认为你需要的是一个共享的xAxis 在那里你应该以另一种方式添加子图。 我通常会这样做,你可以在这里找到subplots_demo.py: http://matplotlib.org/examples/pylab_examples/subplots_demo.html