绘制数据框:覆盖线和条形图不适用于时间序列索引?

plot dataframe: overlay line and bar plot doesn#39;t work for time series index?(绘制数据框:覆盖线和条形图不适用于时间序列索引?)
本文介绍了绘制数据框:覆盖线和条形图不适用于时间序列索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

# create dataframes df0 and df1:
index0 = pd.date_range(start='2014-06-01 00:00:00', end='2014-06-01 00:15:00', freq='1S')          
data0 = np.random.rand(len(index0))                                                                
df0 = pd.DataFrame(data=data0, index=index0, columns=['DF0'])    

index1 = pd.date_range(start='2014-06-01 00:00:00', end='2014-06-01 00:15:00', freq='15S')          
data1 = np.random.rand(len(index1))   
df1 = pd.DataFrame(data=data1, index=index1, columns=['DF1'])                                      

# plot df0 and df1:
fig,ax1 = plt.subplots(figsize=(40,10))
ax2 = ax1.twinx()
df0.plot.line( color="r", ax = ax1)
df1.plot.bar( color ='b', linewidth = 5, ax = ax2, alpha = 0.7)
plt.show()

我可以将数据框叠加为两个线图或两个条形图.但是无论我多么努力,我都无法用条形图或相反的方式覆盖线图?使用上面的代码,我只得到了 df1 的条形图,但看不到 df0 的线图.我有什么不同的做法?

I can overlay the dataframes as two line plots or as two barplots. But however hard I try, I can't manage to overlay a line plot with a bar plot or the other way round? With the code above I only get the barplot of df1 but don't see the lineplot of df0. What do I have to do differently?

推荐答案

bar plot 仅将分类(字符串)值作为 x 值.因此,简单的 hack 可以将时间戳转换为字符串.

bar plot takes categorical (string) values only as the x values. hence simple hack can be converting the time stamps to strings.

当您输入浮点值时,它将它们转换为 str ,因此它们与线图 x 值的索引不匹配.

when you feed the float values, it converts them into str thereby they are not matching with the index of line plot x-values.

df0.index = df0.index.map(str)

此操作也不需要辅助轴.

Secondary axis would also be not required for this.

试试这个!

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

# create dataframes df0 and df1:
index0 = pd.date_range(start='2014-06-01 00:00:00',
                       end='2014-06-01 00:15:00', freq='1S')
data0 = np.random.rand(len(index0))
df0 = pd.DataFrame(data=data0, index=index0, columns=['DF0'])
df0.index = df0.index.map(str)

index1 = pd.date_range(start='2014-06-01 00:00:00',
                       end='2014-06-01 00:15:00', freq='15S')
data1 = np.random.rand(len(index1))
df1 = pd.DataFrame(data=data1, index=index1, columns=['DF1'])

# plot df0 and df1:
fig, ax1 = plt.subplots(figsize=(40, 10))
ax = df0.plot.line(color="r")
df1.plot.bar(color='b', linewidth=5, ax=ax, alpha=0.7)
plt.show()

这篇关于绘制数据框:覆盖线和条形图不适用于时间序列索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Seasonal Decomposition of Time Series by Loess with Python(Loess 用 Python 对时间序列进行季节性分解)
Resample a time series with the index of another time series(使用另一个时间序列的索引重新采样一个时间序列)
How can I simply calculate the rolling/moving variance of a time series in python?(如何在 python 中简单地计算时间序列的滚动/移动方差?)
Python pandas time series interpolation and regularization(Python pandas 时间序列插值和正则化)
Pandas - grouping intra day timeseries by date(Pandas - 按日期对日内时间序列进行分组)
python pandas extract unique dates from time series(python pandas从时间序列中提取唯一日期)