在单个图形中绘制包含HH:MM格式的DataFrames matplotlib [英] Plotting DataFrames containing HH:MM format in a single figure matplotlib

查看:107
本文介绍了在单个图形中绘制包含HH:MM格式的DataFrames matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题(写在下面的末尾)与绘制两个数据框在不同子图(情况1 )中的直方图相比,与在同一图中绘制它们(下面的情况2 ).以1小时的间隔作为分组标准绘制直方图.两个DataFrame都有一列,其时间为"HH:MM"格式.

My question (written at the end below) is related to plotting histograms of two DataFrames in different sub-figures (Situation 1 below) as compared to plotting them in the same figure (Situation 2 below). The histograms are plotted taking an interval of 1 hour as the grouping criteria. Both the DataFrames have a single column with times in "HH:MM" format.

# Defining the two DataFrames
df_in = pd.DataFrame({'time': ['12:20', '12:06', '11:30', '11:03', '10:44', '10:50', '11:52', 
                               '12:21', '9:58', '12:43','12:56', '13:27', '12:14',]})

df_out = pd.DataFrame({'time': ['19:40', '19:44', '19:21', '20:37', '20:27', '18:46', '19:42', 
                                '18:12', '19:08', '21:09', '18:37', '20:34', '20:15']})


情况1 :将两个DataFrame绘制在不同的子图中


Situation 1: Plotting both DataFrames in different sub-figures

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import FixedFormatter

fig, axes = plt.subplots(1, 2, figsize=(9, 3))

colors = ['r', 'b']
titles = ['df-in', 'df-out']

# Looping over the dataframes and plotting them in subfigures
for df, ax, c, t in zip([df_in, df_out], axes.flatten(), colors, titles):
    df['hour'] = pd.to_datetime(df['time'], format='%H:%M')
    df.set_index('hour', drop=False, inplace=True)
    df = df['hour'].groupby(pd.Grouper(freq='60Min')).count()
    df.plot(kind='bar', color=c, ax=ax)
    ticklabels = df.index.strftime('%H:%Mh')
    ax.xaxis.set_major_formatter(FixedFormatter(ticklabels))
    ax.set_title(t, fontsize=18)
plt.show()

情况1的输出

情况2 :在同一图中绘制两个数据框

Situation 2: Plotting both DataFrames in the same figure

fig, axes = plt.subplots(figsize=(7, 3))

# Looping over the dataframes and plotting them in subfigures
for df, c, t in zip([df_in, df_out], colors, titles):
    df['hour'] = pd.to_datetime(df['time'], format='%H:%M')
    df.set_index('hour', drop=False, inplace=True)
    df = df['hour'].groupby(pd.Grouper(freq='60Min')).count()
    df.plot(kind='bar', color=c, ax=axes)
    ticklabels = df.index.strftime('%H:%Mh')
    axes.xaxis.set_major_formatter(FixedFormatter(ticklabels))
plt.show() 

情况2的输出

在两种情况下,用于格式化字符串的代码均来自问题.如您所见,单独绘制时,红色和蓝色直方图在12:00和19:00 h分别具有最大值.但是,当我在同一图中绘制它们时,两个直方图是重叠的,最大值不在12:00和19:00 h.这个问题看似微不足道,但我不确定出了什么问题.

In both the cases, the code for formatting of strings is taken from this question. As you can see, the red and blue Histograms have respective maxima at 12:00 and 19:00 h when plotted separately. But when I plot them in the same plot, the two histograms are overlapping and the maxima are not at 12:00 and 19:00 h. The problem seems to be trivial but I am not sure what is going wrong.

我的问题是:在情况2 中需要修改哪些内容,以使直方图很好地分离和区分(而不是重叠),因为直方图清晰地位于12的中心:00和19:00 h?任何指针和建议,欢迎.

My question is: What needs to be modified in Situation 2 to have both the histograms well separated and distinguishable (instead of overlapping) as they are centered clearly around 12:00 and 19:00 h? Any pointers and suggestions are welcome.

推荐答案

数字条形图可能看起来像这样:

A numeric bar plot could look like this:

import pandas as pd
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
import matplotlib.pyplot as plt
from matplotlib.dates import HourLocator, DateFormatter


# Defining the two DataFrames
df_in = pd.DataFrame({'time': ['12:20', '12:06', '11:30', '11:03', '10:44', '10:50', '11:52', 
                               '12:21', '9:58', '12:43','12:56', '13:27', '12:14',]})

df_out = pd.DataFrame({'time': ['19:40', '19:44', '19:21', '20:37', '20:27', '18:46', '19:42', 
                                '18:12', '19:08', '21:09', '18:37', '20:34', '20:15']})

colors = ['r', 'b']
titles = ['df-in', 'df-out']

fig, ax = plt.subplots(figsize=(7, 3))


for df, c, t in zip([df_in, df_out], colors, titles):
    df['hour'] = pd.to_datetime(df['time'], format='%H:%M')
    df.set_index('hour', drop=False, inplace=True)
    df = df['hour'].groupby(pd.Grouper(freq='60Min')).count()
    df.index = pd.to_datetime(df.index)
    ax.bar(df.index, df.values, width=1/24/2, color=c, label=t)

ax.xaxis.set_major_locator(HourLocator())
ax.xaxis.set_major_formatter(DateFormatter("%H:%Mh"))
ax.set_xlim(pd.to_datetime(["1900-01-01 07:00", "1900-01-01 23:00"]))
plt.setp(ax.get_xticklabels(), rotation=90)
plt.tight_layout()
plt.show() 

这篇关于在单个图形中绘制包含HH:MM格式的DataFrames matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆