Matplotlib 3.0.2使用twinx和主要次要格式化程序和定位器产生了未对齐的日期轴

时间:2019-02-25 15:44:02

标签: python python-3.x pandas numpy matplotlib

我正在尝试绘制时间序列图。我有一个主要和次要的刻度格式化程序和定位器来格式化和定位日期。我的代码是:

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

#Set major and minor tick formatter and locators
major_loc = mdates.WeekdayLocator(byweekday=(0), interval=1)
major_fmt = mdates.DateFormatter('%d-%b')
minor_loc = mdates.DayLocator()

# Read Sample CSV
df = pd.read_csv('https://raw.githubusercontent.com/APophale/sfm-shiny/master/data.csv', index_col=['Date'], parse_dates=['Date'])
month_yr = df.index[0].strftime('%b-%Y')
dates = df.index.to_pydatetime()

fig = plt.figure(figsize=(17,11))

#Plot flow
ax_flow = plt.subplot(411)
ax_flow.plot_date(dates, df['Flow (MGD)'], '-', linewidth=1.0, color='black')
ax_flow.xaxis.set_major_locator(major_loc)
ax_flow.xaxis.set_major_formatter(major_fmt)
ax_flow.xaxis.set_minor_locator(minor_loc)
ax_flow.grid('on', which='both', linestyle='--', linewidth=0.75)
ax_flow.set_ylabel('Flow (MGD)')
ax_flow.set_title('RGCOBF1 Midway Rd', loc='right')

#Plot rainfall
ax_rain = ax_flow.twinx()
ax_rain.bar(dates, df['Rainfall (inches)'], width=0.10)
ax_rain.xaxis.set_major_locator(major_loc)
ax_rain.xaxis.set_major_formatter(major_fmt)
ax_rain.xaxis.set_minor_locator(minor_loc)
ax_rain.set_ylim(0, 1)
ax_rain.invert_yaxis()
ax_rain.set_ylabel('Rainfall (inches)')

#Plot depth
ax_dep = plt.subplot(412)
ax_dep.plot_date(dates, df['Depth (inches)'], '-', linewidth=1.0, color='red')
ax_dep.xaxis.set_major_locator(major_loc)
ax_dep.xaxis.set_major_formatter(major_fmt)
ax_dep.xaxis.set_minor_locator(minor_loc)
ax_dep.grid('on', which='both', linestyle='--', linewidth=0.75)
ax_dep.set_ylabel('Depth (inches)')
ax_dep.tick_params(axis='y', colors='red')

#Plot velocity
ax_vel = ax_dep.twinx()
ax_vel.plot_date(dates, df['Velocity (ft/s)'], '--', linewidth=1.0, color='green')
ax_vel.xaxis.set_major_locator(major_loc)
ax_vel.xaxis.set_major_formatter(major_fmt)
ax_vel.xaxis.set_minor_locator(minor_loc)
ax_vel.set_ylabel('Velocity (ft/s)')
ax_vel.tick_params(axis='y', colors='green')

#Plot scatter
ax_scatter = plt.subplot(413)
ax_scatter.scatter(x=df['Velocity (ft/s)'], y=df['Depth (inches)'], s=5)
ax_scatter.set_ylabel('Depth (inches)')
ax_scatter.set_xlabel('Velocity (ft/s)')

#Plot table
ax_table = plt.subplot(414)
rows = ('Min', 'Max', 'Average', 'Total')
columns = ('Flow (MGD)', 'Rainfall (inches)', 'Velocity (ft/s)', 'Depth (inches)')
table_data = [[np.around(df['Flow (MGD)'].min(), 3), '', np.around(df['Velocity (ft/s)'].min(), 3),
               np.around(df['Depth (inches)'].min(), 3)],
              [np.around(df['Flow (MGD)'].max(), 3), '', np.around(df['Velocity (ft/s)'].max(), 3),
               np.around(df['Depth (inches)'].max(), 3)],
              [np.around(df['Flow (MGD)'].mean(), 3), '', np.around(df['Velocity (ft/s)'].mean(), 3),
               np.around(df['Depth (inches)'].mean(), 3)],
              [np.around(df['Flow (MGD)'].sum()*0.0104167, 3), np.around(df['Rainfall (inches)'].sum(), 3),
               '', '']
              ]

ax_table.axis('tight')
ax_table.axis('off')
table = ax_table.table(cellText=table_data, rowLabels=rows, colLabels=columns, loc='center', cellLoc='center')
table.set_fontsize(10)
table.scale(1, 1.5)
fig.subplots_adjust(hspace=0.15, top=0.92)

plt.plot()

我的输出看起来像这样。您可以在顶部图表中看到日期轴未对齐。我正在使用matplotlib 3.0.2。我不知道怎么了。我尝试注释掉ax_rain的格式化程序和定位符,但是它不起作用。

Result

0 个答案:

没有答案