【问题标题】:python, using seaborn or plotly to show graphs for medication points over timepython,使用 seaborn 或 plotly 显示药物治疗点随时间变化的图表
【发布时间】:2021-07-21 11:47:58
【问题描述】:

我正在尝试绘制我喝咖啡时的心率和步数。

数据集是这样的,

    heartRate   steps   coffee
created         
2020-04-14 06:03:00 71.0    NaN 0
2020-04-14 09:03:00 72.0    NaN 1
2020-04-14 09:55:00 61.0    NaN 1
2020-04-14 09:58:00 67.0    NaN 1
2020-04-14 10:01:00 82.0    NaN 2

其中 1 是我喝咖啡的小时,2 是 4 小时后,0 是没有咖啡。

目前我正在尝试像这样绘制这些点,

import seaborn as sns
sns.set_theme(style="darkgrid")

# Load an example dataset with long-form data
fmri = tester

# Plot the responses for different events and regions
sns.lineplot(x="created", y="heartRate", style="medication",
             data=fmri)

但这给了我这样一个难以理解的图表,

我希望它更具可读性 - 我在这个库中做错了什么?

【问题讨论】:

    标签: python plotly seaborn


    【解决方案1】:

    也许最好使用 matplotlib。在那里,您可以使用图形大小、标记和线宽来优化和调整。

    import matplotlib.pyplot as plt
    from matplotlib import rc
    import pandas as pd
    
    import numpy as np
    from datetime import datetime, timedelta as delta
    
    font = {'family' : 'Arial',
            'weight' : 'normal',
            'size'   : 60}    
    rc('font', **font)    
    ndays = 1000
    start = datetime(2018, 12, 1)
    dates = [start - delta(days = x) for x in range(0, ndays)]
    
    df = pd.DataFrame({'time':dates,
                       'heartrate':np.random.randint(60,70,len(dates)),
                       'coffee':np.random.randint(0,2,len(dates))})
    
    df['heartrate_coff'] = df['heartrate']
    df['heartrate_nocoff'] = df['heartrate']
    df.loc[df['coffee'].values==0,['heartrate_coff'] ]= np.nan
    df.loc[df['coffee'].values==1,['heartrate_nocoff']]= np.nan
    
    fig = plt.figure(num=None, figsize=(200, 10), 
                     dpi=9, facecolor='w', edgecolor='k')
    plt.plot(df['time'].values , df['heartrate'].values,'k-', linewidth=10)   
    plt.plot(df['time'].values , df['heartrate_coff'].values,'rd',markersize=30)   
    plt.plot(df['time'].values , df['heartrate_coff'].values,'rd-', linewidth=20)   
    plt.plot(df['time'].values , df['heartrate_nocoff'].values,'ko',markersize=10)   
    plt.xticks(rotation=-10)
    

    【讨论】:

      猜你喜欢
      • 2013-04-24
      • 1970-01-01
      • 2021-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-26
      • 2021-05-14
      • 1970-01-01
      相关资源
      最近更新 更多