在单个数据框中,为每一天绘制每天的逐小时测量结果,每天一个小图。

huangapple go评论55阅读模式
英文:

plotting hourly measurements per day in subplots for each day, with data from a single dataframe

问题

以下是您要翻译的代码部分:

I have a dataframe with a single hourly measurement for an entire month and I'm trying to plot each day's measurement as a function of time, in a separate subplot for each day. The dataframe looks like this.

                                   Hourly_diff
    dateTimeUtc                            
    2022-09-01 00:00:00+00:00         18.0
    2022-09-01 01:00:00+00:00        -20.0
    2022-09-01 02:00:00+00:00         56.0
    2022-09-01 03:00:00+00:00        141.0
    ...
    2022-10-01 18:00:00+00:00       -465.0
    2022-10-01 19:00:00+00:00       -617.0
    2022-10-01 20:00:00+00:00        173.0
    2022-10-01 21:00:00+00:00        378.0

Ideally, I'd get a 6 x 5 grid (for a 30-day month) and plot it all by looping through the dataframe to plot every 24 entries. I've tried several ways which I won't bore you with, and none worked sufficiently, so now I'm asking for advice, what's a smart way of doing it?

So far I've tried the following:

    date = '22-09'
    Zone = '1' # There will be multiple zones eventually
    data['Hourly_diff'] = data['GNP'+Zone]-data[Zone]
    data.set_index('dateTimeUtc', inplace=True)
    data = pd.DataFrame(data['Hourly_diff'])
    data = data.iloc[2:,:] # I need to remove the first two entries

    df = np.array_split(data, 31) # not sure if this is a good idea or not?

    # fig, ax  = plt.subplots()
    # ax.plot(df[0].index, df[0])

    fig, axes = plt.subplots(nrows=6, ncols=6, figsize=(15, 7), sharex=False, sharey=False)

    for i, axs in zip(df, axes):
        axs.plot(df[i].index, df[i])

    plt.show

When I run this for a single entry, as in the commented out part below,

    # fig, ax  = plt.subplots()
        # ax.plot(df[0].index, df[0])

it works fine, but when I try to put it in a loop I get an attribute error which I don't get for just printing individual elements.

    AttributeError: 'numpy.ndarray' object has no attribute 'plot'

I'm sure there's a better way of doing this; any advice is much appreciated.

请注意,我只翻译了代码部分,去除了其他内容。

英文:

I have a dataframe with a single hourly measurement for an entire month and I'm trying to plot each days measurement as a function of time, in a separate subplot for each day. The dataframe looks like this.

                               Hourly_diff
dateTimeUtc                           
2022-09-01 00:00:00+00:00         18.0
2022-09-01 01:00:00+00:00        -20.0
2022-09-01 02:00:00+00:00         56.0
2022-09-01 03:00:00+00:00        141.0
...
2022-10-01 18:00:00+00:00       -465.0
2022-10-01 19:00:00+00:00       -617.0
2022-10-01 20:00:00+00:00        173.0
2022-10-01 21:00:00+00:00        378.0

Ideally I'd get a 6 x 5 grid (for a 30 day month) and plot it all by looping through the dataframe to plot every 24 entries. I've tried several ways which I won't bore you with and none worked sufficiently so now I'm asking for advice, whats a smart way of doing it?

So far I've tried the following:

date = '22-09'
Zone = '1' # There will be multiple zones eventually 
data['Hourly_diff'] = data['GNP'+Zone]-data[Zone]
data.set_index('dateTimeUtc', inplace=True)
data = pd.DataFrame(data['Hourly_diff'])
data = data.iloc[2:,:] # I need to remove the first two entries
df = np.array_split(data,31) # not sure if this is a good idea or not?
# fig, ax  = plt.subplots()
# ax.plot(df[0].index,df[0])
fig, axes = plt.subplots(nrows=6, ncols=6, figsize=(15, 7), sharex=False, sharey=False)
for i,axs in zip(df,axes):
axs.plot(df[i].index,df[i])
plt.show

When I run this for a single entry, as in the commented out part below,

# fig, ax  = plt.subplots()
# ax.plot(df[0].index,df[0])

it works fine, but when I try to put it in a loop I get an attribute error which I don't get for just printing individual elements.

AttributeError: 'numpy.ndarray' object has no attribute 'plot'

I'm sure there's a better way of doing this, any advice much appreciated.

答案1

得分: 0

你可以尝试类似这样的代码:

df['date'] = df['dateTimeUtc'].dt.date
dates = df['date'].unique().tolist()
fig, axs = plt.subplots(nrows=6, ncols=5, figsize=(18, 12))

dates_iterator = 0
for i in range(6):
    for j in range(5):
        axs[i][j].plot(
            df.loc[df['date'] == dates[dates_iterator], 'dateTimeUtc'],
            df.loc[df['date'] == dates[dates_iterator], 'hourly_diff']
        )
        dates_iterator += 1
英文:

You can try something like this:

df['date'] = df['dateTimeUtc'].dt.date
dates = df['date'].unique().tolist()
fig, axs = plt.subplots(nrows=6, ncols=5, figsize=(18,12))
dates_iterator = 0
for i in range(6):
for j in range(5):
axs[i][j].plot(
df.loc[df['date']==dates[dates_iterator], 'dateTimeUtc'],
df.loc[df['date']==dates[dates_iterator], 'hourly_diff']
)
dates_iterator += 1

答案2

得分: 0

所以事实证明,我试图在一个数据框内进行迭代,只需展平轴以访问信息。问题是由于使用了分割来设置数据框。问题已解决如下:

df = np.array_split(data, 31)

for i, ax in enumerate(axes.flatten()):
    if i < len(df):
        ax.plot(df[i].index, df[i])
    else:
        ax.axis('off')
英文:

So it turns out I was trying to iterate through a dataframe within a dataframe, and just needed to flatten the axes to access the info. The problem came from setting up the df with a split. It was solved like this:

df = np.array_split(data,31)
for i, ax in enumerate(axes.flatten()):
if i &lt; len(df):
ax.plot(df[i].index, df[i])
else:
ax.axis(&#39;off&#39;)

huangapple
  • 本文由 发表于 2023年2月27日 06:04:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575280.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定