Search code examples
pythonpandasmatplotlibdataframeastronomy

Plotting multiple lines from columns of Dataframe (python/pandas)?


I have large (~1 million) data set that looks something like this...

Age    Wavelength    Luminosity

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

I have it in a dataframe currently and want to plot wavelength vs Luminosity for all age groups. How do i get plots for each age on one graph?


Solution

  • You can use 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