【问题标题】:How to exclude San and Sat from matplotlib graph? [duplicate]如何从 matplotlib 图中排除 San 和 Sat? [复制]
【发布时间】:2013-09-24 11:28:40
【问题描述】:

我有这样的熊猫数据框:

2013-09-13 20:51:00
2013-09-13 20:52:00
2013-09-13 20:53:00
2013-09-13 20:54:00
2013-09-13 20:55:00
2013-09-13 20:56:00
2013-09-13 20:57:00
2013-09-13 20:58:00
2013-09-13 20:59:00
2013-09-16 00:00:00
2013-09-16 00:01:00
2013-09-16 00:02:00
2013-09-16 00:03:00
2013-09-16 00:04:00
2013-09-16 00:05:00
2013-09-16 00:06:00
2013-09-16 00:07:00
2013-09-16 00:08:00

没有周日和周六日期。 Matplotlib pyplot 是这样画的:

除了 emty date 之外,是否可以裁剪图像?像这样:

【问题讨论】:

  • This post 已满或建议使用不连续的轴。

标签: python matplotlib pandas


【解决方案1】:

如果您在 matplotlib 中绘制具有空值(或掩码数组)的序列,则该行将在空值位置断开。

例如:

import matplotlib.pyplot as plt
import numpy as np

plt.plot([1, 2, 3, np.nan, np.nan, 6, 7, 8, 9])
plt.show()

因此,您要做的是为周末日期添加NaNpandas 提供了几种不同的方法来做到这一点。有an overview here,但基本上你想看看asfreqresample方法。

以您的日期为例:

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

dates = """2013-09-13 20:51:00
2013-09-13 20:52:00
2013-09-13 20:53:00
2013-09-13 20:54:00
2013-09-13 20:55:00
2013-09-13 20:56:00
2013-09-13 20:57:00
2013-09-13 20:58:00
2013-09-13 20:59:00
2013-09-16 00:00:00
2013-09-16 00:01:00
2013-09-16 00:02:00
2013-09-16 00:03:00
2013-09-16 00:04:00
2013-09-16 00:05:00
2013-09-16 00:06:00
2013-09-16 00:07:00
2013-09-16 00:08:00"""
dates = pd.to_datetime(dates.split('\n'))

# Generate some random y-data...
y = (np.random.random(dates.size) - 0.5).cumsum()

# Resample to a regular 1-minute interval series
# By default, this will put NaNs in where there isn't any data...
data = pd.Series(y, index=dates).asfreq('1Min')

data.plot().set(title='Resampled data')
plt.show()

【讨论】:

    猜你喜欢
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 2011-01-31
    • 1970-01-01
    • 2020-04-08
    相关资源
    最近更新 更多