【问题标题】:Plotting multiple lines from columns of Dataframe (python/pandas)?从数据框(python/pandas)的列中绘制多条线?
【发布时间】:2016-11-06 08:59:55
【问题描述】:

我有大型(约 100 万)数据集,看起来像这样......

Age    Wavelength    Luminosity

1      96            100
1      97            150
1      98            100
2      96.5          90
2      97            160
2      97.5          120
...

我目前将它放在一个数据框中,并希望绘制所有年龄段的波长与光度的关系。如何在一张图上绘制每个年龄的图?

【问题讨论】:

标签: python pandas matplotlib dataframe astronomy


【解决方案1】:

您可以使用pandas.core.groupby.DataFrameGroupBy.plot

In[1]: import pandas as pd

In[2]: import numpy as np

In[3]: dataset = pd.DataFrame({"Age": np.random.random_integers(1, 5, 100), "Wavelength": np.random.uniform(90, 100, 100), "Luminosity": np.random.random_integers(5, 15, 100) * 10})

In[4]: dataset.groupby("Age").plot(x="Wavelength", y="Luminosity", kind="scatter")
Out[4]: 
Age
1    Axes(0.125,0.1;0.775x0.8)
2    Axes(0.125,0.1;0.775x0.8)
3    Axes(0.125,0.1;0.775x0.8)
4    Axes(0.125,0.1;0.775x0.8)
5    Axes(0.125,0.1;0.775x0.8)
dtype: object

【讨论】:

  • 谢谢!有没有办法将所有这些都放在一个图表上?
  • plot 函数返回一个 matplotlib AxesSubplot 对象。所以你可以使用matplotlib subplot 网格和这个轴对象。不过我没试过。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-16
  • 2012-06-16
  • 2021-12-10
  • 2017-02-25
相关资源
最近更新 更多