【问题标题】:How to plot longitudinal data / repeated measures in python using matplotlib?如何使用matplotlib在python中绘制纵向数据/重复测量?
【发布时间】:2016-05-26 22:57:20
【问题描述】:

如何在 python 中使用 matplotlib 等绘制纵向数据。

from pandas import DataFrame
from scipy.stats import norm
from scipy.stats import randint
import numpy as np
import pandas as pd

# some sample data
df = DataFrame({
    'one' : norm.rvs(loc=12, size=10),
    'two' : norm.rvs(loc=5, size=10),
    'three' : norm.rvs(loc=7, size=10),
    'four' : norm.rvs(loc=3, size=10),
    'type' : ['type-%i' % i for i in randint.rvs(0,3,size=10)]})

如何在 x 轴上绘制重复测量的标签:1-2-3-4,并为 10 个样本中的每一个绘制一条连接它们各自值的线?如何按类型为每个样本着色?

【问题讨论】:

    标签: python pandas matplotlib plot seaborn


    【解决方案1】:

    连接线的部分我没看懂,但也许你想要的是类似于这样的东西:

    fig, ax = plt.subplots( 1, 1 )
    
    ax.plot( np.zeros(len(df.one))+1, df.one, '.' )
    ax.plot( np.zeros(len(df.two)) + 2, df.two, '.' )
    ax.plot( np.zeros(len(df.three)) + 3 , df.three, '.' )
    ax.plot( np.zeros(len(df.four)) + 4, df.four, '.' )
    
    ax.set_xticks([1,2,3,4]);
    ax.set_xticklabels(( 'one', 'two', 'three', 'four' ))
    ax.set_xlim([0,5]);
    

    哪些结果:

    【讨论】:

      猜你喜欢
      • 2017-07-06
      • 2014-11-22
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-19
      • 2014-05-29
      相关资源
      最近更新 更多