【问题标题】:Plotting error with matplotlib (no x-tick label)matplotlib 绘图错误(无 x-tick 标签)
【发布时间】:2017-03-18 08:15:46
【问题描述】:

我正在尝试绘制以下熊猫数据框(第一列是索引):

          pressure mean  pressure std     Time
  Time                                          
00:00:00     618.663133      0.108484 00:00:00
01:00:00     618.281129      0.111788 01:00:00
02:00:00     617.975430      0.101657 02:00:00
03:00:00     617.766129      0.062712 03:00:00
04:00:00     617.807043      0.066030 04:00:00
05:00:00     617.976479      0.079755 05:00:00
06:00:00     618.276720      0.118224 06:00:00
.........

我的目标是绘制时间(x 轴)与压力平均值(y 轴)的关系。 我希望 x 轴具有 2 小时刻度和 H:M 格式。 我正在使用以下代码:

%matplotlib inline  

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
from matplotlib.ticker import MultipleLocator, FormatStrFormatter



fig = plt.figure(figsize=(15,7))
ax = fig.add_subplot(1,1,1) # row-col-num
# --- line plot data on the Axes
ax.plot(df6['Time'], df6['pressure mean'], 'b-', linewidth=2,
label=r'Hawaii Pressure')
# -- Tick Label Size --
plt.rcParams['xtick.labelsize'] = 12 
plt.rcParams['ytick.labelsize'] = 16
# -- Tick Labels Format --
DaysFmt = mdates.DateFormatter('%H:%M')
minorLocator = MultipleLocator(5)
ax.xaxis.set_minor_locator(minorLocator)
ax.xaxis.set_major_formatter(DaysFmt)

# -- Axis Labels --
ax.set_ylabel(r'Pressure $[mb]$', fontsize=22)
ax.set_xlabel(r'Hour', fontsize=22)
# -- Tick Limits and Labels --
ax.xaxis.set_major_locator(mdates.HourLocator(interval=2))              

# -- Legend and Grid --
ax.legend(loc='best')
ax.grid(True)
# -- Title --
fig.suptitle('Stacked Hourly Pressure Averages (1 month)',fontsize=27)
fig.tight_layout(pad=5)
# -- Intervals --
fig.savefig('Hawaii Stacked Pressure October 2016.png', dpi=300)

但是我遇到了内存错误,我知道这是因为以下行:

ax.xaxis.set_minor_locator(minorLocator)

如果我删除这条线 - 我得到一个没有任何 x 轴刻度标签的图!

有什么建议吗? 谢谢!!

【问题讨论】:

标签: python pandas matplotlib plot


【解决方案1】:

找到了解决办法!

我只需要添加时间字符串列到日期时间的转换:

df6['Time'] = pd.to_datetime(df6['Time'])

耶!

【讨论】:

    猜你喜欢
    • 2014-12-29
    • 1970-01-01
    • 2020-09-17
    • 2013-06-14
    • 1970-01-01
    • 2019-02-26
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多