不能使日期出现在pyplot的x轴上

时间:2017-09-16 11:50:51

标签: python-3.x date matplotlib graph

所以我一直试图绘制一些数据。我从数据库中获取数据并将其全部正确放入变量text_。这是代码的片段:

import sqlite3
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from dateutil.parser import parse

fig, ax = plt.subplots()

# Twin the x-axis twice to make independent y-axes.
axes = [ax, ax.twinx(), ax.twinx()]

# Make some space on the right side for the extra y-axis.
fig.subplots_adjust(right=0.75)

# Move the last y-axis spine over to the right by 20% of the width of the axes
axes[-1].spines['right'].set_position(('axes', 1.2))

# To make the border of the right-most axis visible, we need to turn the frame on. This hides the other plots, however, so we need to turn its fill off.
axes[-1].set_frame_on(True)
axes[-1].patch.set_visible(False)

# And finally we get to plot things...
text_ = [('01/08/2017', 6.5, 143, 88, 60.2, 3), ('02/08/2017', 7.0, 146, 90, 60.2, 4), 
         ('03/08/2017', 6.7, 142, 85, 60.2, 5), ('04/08/2017', 6.9, 144, 86, 60.1, 6),
         ('05/08/2017', 6.8, 144, 88, 60.2, 7), ('06/08/2017', 6.7, 147, 89, 60.2, 8)]
colors = ('Green', 'Red', 'Blue')
label = ('Blood Sugar Level (mmol/L)', 'Systolic Blood Pressure (mm Hg)', 'Diastolic Blood Pressure (mm Hg)')

y_axisG = [text_[0][1], text_[1][1], text_[2][1], text_[3][1], text_[4][1], text_[5][1]] #Glucose data
y_axisS = [text_[0][2], text_[1][2], text_[2][2], text_[3][2], text_[4][2], text_[5][2]] # Systolic Blood Pressure data
y_axisD = [text_[0][3], text_[1][3], text_[2][3], text_[3][3], text_[4][3], text_[5][3]] # Diastolic Blood Pressure data


AllyData = [y_axisG, y_axisS, y_axisD] #list of the lists of data

dates = [text_[0][0], text_[1][0], text_[2][0], text_[3][0], text_[4][0], text_[5][0]] # the dates as strings
x_axis = [(parse(x, dayfirst=True)) for x in dates] #converting the dates to datetime format for the graph

Blimits = [5.5, 130, 70] #lower limits of the axis
Tlimits = [8, 160, 100] #upper limits of the axis

for ax, color, label, AllyData, Blimits, Tlimits in zip(axes, colors, label, AllyData, Blimits, Tlimits):
    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y')) #format's the date
    plt.gca().xaxis.set_major_locator(mdates.DayLocator())
    data = AllyData
    ax.plot(data, color=color) #plots all the y-axis'
    ax.set_ylim([Blimits, Tlimits]) #limits
    ax.set_ylabel(label, color=color) #y-axis labels
    ax.tick_params(axis='y', colors=color)
axes[0].set_xlabel('Date', labelpad=20)
plt.gca().set_title("Last 6 Month's Readings",weight='bold',fontsize=15)

plt.show()

目前代码生成此图表: Graph with no x-values

我理解问题可能在ax.plot部分,但我不确定究竟是什么。我尝试将这行代码放在ax.plot(data, x_axis, color=color但是,这使得整个图形都搞砸了,并且日期没有显示在x轴上,就像我想要的那样。

有什么我错过的吗? 如果这已在其他地方得到解答,请告诉我如何通过编辑我的代码将其实现到我的代码中?

非常感谢

1 个答案:

答案 0 :(得分:2)

显然x_data实际上从未在代码中使用过。而不是

ax.plot(data, color=color)

data与其索引相对应,您可能希望根据x_axis中存储的日期绘制数据。

ax.plot(x_axis, data, color=color)

最后,在plt.gcf().autofmt_xdate()之前添加plt.show会很好地轮换日期,这样它们就不会重叠。

enter image description here