如何重新创建此 pandas 数据框,线条和条形图 [英] How can I recreate this plot of a pandas DataFrame, line and bar

查看:35
本文介绍了如何重新创建此 pandas 数据框,线条和条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前,我设法创建了以下情节

将pandas导入为pd导入matplotlib.pyplot作为pltdf_prog = pd.DataFrame({"Prognos tim":[2,3,3]})df_prog.index = pd.date_range(start ='2020-01-01 00',end ='2020-01-01 02',freq ='H')df_prog.index = df_prog.index + pd.Timedelta(分钟= 30)

现在我试图再次创建这个情节,但没有成功.我的记忆让我失望

我试过了

  ax = df_prog.plot(kind ='bar')df_prog.plot(kind='line')

,如

Previously I managed to create the following plot

import pandas as pd
import matplotlib.pyplot as plt

df_prog = pd.DataFrame({"Prognos tim": [2, 3, 3]})
df_prog.index = pd.date_range(start='2020-01-01 00', end='2020-01-01 02', freq='H')
df_prog.index = df_prog.index + pd.Timedelta(minutes=30)

Now I'm trying to once again create this plot but without success. My memory is failing me

I've tried

ax = df_prog.plot(kind='bar')
df_prog.plot(kind='line')

as described in Plot Pandas DataFrame as Bar and Line on the same one chart

But depending on which is chosen first, bar or line, only one is showed, and not both in the same figure.

解决方案

You need to convert the time axis to string. Then you can plot them together.

import pandas as pd
import matplotlib.pyplot as plt

df_prog = pd.DataFrame({"Prognos tim": [2, 3, 3]})
df_prog.index = pd.date_range(start='2020-01-01 00', end='2020-01-01 02', freq='H')
df_prog.index = df_prog.index + pd.Timedelta(minutes=30)

_, ax = plt.subplots()
df_prog.index = df_prog.index.astype(str)
df_prog.plot(kind='line', linestyle='-', marker='o', color='r', ax=ax)
df_prog.plot(kind='bar', ax=ax)

plt.show()

这篇关于如何重新创建此 pandas 数据框,线条和条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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