0

I'm running a lot of simulations in the field of electrical systems using PSS/E. Currently, I'm on the phase of treating the captured data to visualize it.

I have already investigated over the Internet. I've also seen a lot of examples on how to build different plots using ggplot in python, but none of those tutorials or examples seem to care much about the dataframe construction.

The following code does plot a chart as expected for me.

    def plot_default_example(self):
        islands = ['LPA', 'LPA', 'LPA', 'LPA', 'LPA', 'LPA',
                   'TNF', 'TNF', 'TNF', 'TNF', 'TNF', 'TNF',
                   'GOM', 'GOM', 'GOM', 'GOM', 'GOM', 'GOM']
        years = [2014, 2015, 2016, 2017, 2018, 2019,
                 2014, 2015, 2016, 2017, 2018, 2019,
                 2014, 2015, 2016, 2017, 2018, 2019]
        population = []
        for _ in range(0, 18):
            population.append(randint(1500, 15000))
        print years
        print population
        df = pd.DataFrame({'island': islands, 'years': years, 'population': population})
        plt = ggplot(df, aes(x='years', y='population', color='island')) +\
            geom_line() + labs(title='Random Population in 3 of the Canary Islands from 2014 to 2019')
        print plt

plot of random population over a few years in 3 of the canary islands

As you might realize, in the dataframe there is a lot of redundant because the years and islands are repeated, but it is the only way I know how to do it.

I was wondering if there is any way to avoid such redundancy.

Thanks for any help you can provide.

0

I have figured it out.

df = pd.DataFrame({'years': [2014, 2015, 2016, 2017, 2018, 2019],
                           'Population_GOM': [155, 785, 452, 958, 254, 635],
                            'Population_LPA': [777, 444, 123, 734, 697, 148],
                            'Population_TNF': [1005, 85, 321, 478, 307, 448]
                           })


        df = pd.melt(df, id_vars='years')
        print df
        plt = ggplot(aes(x='years', y='value', color='variable'), df) + \
            geom_line()
        print plt

Being the output, (note that is not the same population data)

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.