从数据帧在 Python/matplotlib 中的条形图顶部绘制线图 [英] Plotting line plot on top of bar plot in Python / matplotlib from dataframe

查看:32
本文介绍了从数据帧在 Python/matplotlib 中的条形图顶部绘制线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在matplotlib中的堆积条形图上绘制线形图,但无法同时显示它们.

I am trying to plot a line plot on top of a stacked bar plot in matplotlib, but cannot get them both to show up.

我已经通过从其他数据帧和日期时间索引中提取各种信息来设置组合数据帧.我试图从活动列(LightlyActive、FairlyActive、VeryActive)和每个睡眠周期(wake、light、deep、rem)放在一组轴( ax1 )上.然后,我尝试将 efficiency 列绘制为一组单独的轴 (ax2) 上的线图.

I have the combined dataframe already set up by pulling various information from other dataframes and with the datetime index. I am trying to plot a stacked bar plot from the activity columns (LightlyActive, FairlyActive, VeryActive) and several line plots from the minutes in each sleep cycle (wake, light, deep, rem) on one set of axes (ax1). I am then trying to plot the efficiency column as a line plot on a separate set of axes (ax2).

我无法同时显示堆积条形图和折线图.如果我第二次绘制条形图,那是唯一显示的条形图.如果我先绘制线图(活动性和效率),则这些是唯一显示的线形图.似乎我绘制的第二个情节的风格覆盖了第一个.

I cannot get both the stacked bar plot and the line plots to show up simultaneously. If I plot the bar plot second, that is the only one that shows up. If I plot the line plots first (activity and efficiency) those are the only ones that show up. It seems like whichever style of plot I plot second covers up the first one.

            LightlyActive  FairlyActive  VeryActive  efficiency  wake  light   deep    rem
dateTime                                                                                  
2018-04-10            314            34         123        93.0  55.0  225.0   72.0   99.0
2018-04-11            253            22         102        96.0  44.0  260.0   50.0   72.0
2018-04-12            282            26          85        93.0  47.0  230.0   60.0   97.0
2018-04-13            292            35          29        96.0  43.0  205.0   81.0   85.0

fig, ax1 = plt.subplots(figsize = (10, 10))
temp_df[['LightlyActive', 'FairlyActive', 'VeryActive']].plot(kind = 'bar', stacked = True, ax = ax1)
ax2 = plt.twinx(ax = ax1)
temp_df[['wake', 'light', 'deep', 'rem']].plot(ax = ax1)
temp_df['efficiency'].plot(ax = ax2)
plt.show()

我想在单个图上绘制活动水平('LightlyActive'、'FairlyActive'、'VeryActive')和睡眠周期('wake'、'light'、'deep'、'rem')在一组轴上,而睡眠效率在第二组轴上.

I would like to have on single plot with a stacked bar plot of activity levels ('LightlyActive', 'FairlyActive', 'VeryActive') and sleep cycles ('wake', 'light', 'deep', 'rem') on one set of axes, and sleep efficiency on a second set of axes.

我什至没有像下面的编辑版本中的特伦顿那样显示它(称为特伦顿M编辑").紧接其下方的 2 个图是为我显示的版本.

I am not even getting it to display as Trenton did in the edited version below (designated as "Edited by Trenton M"). The 2 plots immediately below this are the versions that display for me.

  • 注意圆圈区域.

推荐答案

想通了!通过将日期保留为一列(即将其设置为索引),我既可以绘制线条图,也可以绘制条形图.然后,我可以返回并相应地调整标签.

Figured it out! By leaving the dates as a column (i.e. not setting them as the index), I can plot both the line plot and bar plot. I can then go back and adjust labels accordingly.

@ScottBoston,您的x轴提示了我.感谢您对此进行调查.

@ScottBoston your x-axis tipped me off. Thanks for looking into this.

date1 = pd.datetime(2018, 4, 10)
data = {'LightlyActive': [314, 253, 282, 292],
    'FairlyActive': [34, 22, 26, 35],
    'VeryActive': [123, 102, 85, 29],
    'efficiency': [93.0, 96.0, 93.0, 96.0],
    'wake': [55.0, 44.0, 47.0, 43.0],
    'light': [225.0, 260.0, 230.0, 205.0],
    'deep': [72.0, 50.0, 60.0, 81.0],
    'rem': [99.0, 72.0, 97.0, 85.0],
    'date': [date1 + pd.Timedelta(days = i) for i in range(4)]}
temp_df = pd.DataFrame(data)

fig, ax1 = plt.subplots(figsize = (10, 10))
ax2 = plt.twinx(ax = ax1)
temp_df[['LightlyActive', 'FairlyActive', 'VeryActive']].\
         plot(kind = 'bar', stacked = True, ax = ax1)
temp_df[['wake', 'light', 'deep', 'rem']].plot(ax = ax1, alpha = 0.5)
temp_df['efficiency'].plot(ax = ax2)
ax1.set_xticklabels(labels = temp_df['date'])
plt.show()

这篇关于从数据帧在 Python/matplotlib 中的条形图顶部绘制线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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