【问题标题】:How to plot values from the DataFrame? Python 3.0如何从 DataFrame 中绘制值?蟒蛇3.0
【发布时间】:2018-01-16 20:33:11
【问题描述】:

我正在尝试根据索引(DataFrame 表的)绘制 A 列中的值,但它不允许我这样做。怎么做?

INDEX 是来自 DataFrame 的索引,而不是声明的变量。

【问题讨论】:

    标签: dataframe pythonplotter


    【解决方案1】:

    您只需要绘图列A,索引用于xy 的值默认用于Series.plot

    #line is default method, so omitted
    Test['A'].plot(style='o')
    

    另一种解决方案是reset_index 来自index 的列,然后是DataFrame.plot

    Test.reset_index().plot(x='index', y='A', style='o')
    

    示例:

    Test=pd.DataFrame({'A':[3.0,4,5,10], 'B':[3.0,4,5,9]})
    print (Test)
          A    B
    0   3.0  3.0
    1   4.0  4.0
    2   5.0  5.0
    3  10.0  9.0
    
    Test['A'].plot(style='o')
    


    print (Test.reset_index())
       index     A    B
    0      0   3.0  3.0
    1      1   4.0  4.0
    2      2   5.0  5.0
    3      3  10.0  9.0
    
    Test.reset_index().plot(x='index', y='A', style='o')
    

    【讨论】:

    • 非常感谢您的帮助。如果我想让 y 轴从 0 到 10,我该怎么做?
    • 你需要ax = Test['A'].plot(style='o') ax.set_ylim(0,10) - 见here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-28
    • 2019-04-14
    • 1970-01-01
    • 2010-10-27
    相关资源
    最近更新 更多