Skip to content Skip to sidebar Skip to footer

Creating Horizontal Bar Plot With Time-series Data In Python

I have the following problem: Given a pandas dataframe with a number of unique hostnames, I would like to plot a horizontal bar graph that indicates the length of time that a parti

Solution 1:

I think you don't need a bar plot, because it is used for visualizing relative distribution of categorical data. One solution could be using the following approach. Lets suppose that we have your test data in csv format.

In [1]: import pandas as pd
        import matplotlib.pyplot as plt
        df = pd.read_csv("df.txt", parse_dates = ["Created", "Resolved"], index_col = "Summary")
        df = df.stack().reset_index().rename(columns={0:"date"}).set_index("date")[["Summary"]]
        df = pd.get_dummies(df).applymap(lambda x:x if x else pd.np.nan)
        for n, col inenumerate(df.columns): df[col] = df[col]*n
        df.plot(lw=10, legend=False)
        plt.yticks(pd.np.arange(len(df.columns)), df.columns)
        plt.tight_layout()
        plt.show()

Basically, what the code above does is convert the "Created and Resolved" columns in the index of a new dataframe, then assign numerical values to each event when occurs or NaN if doesn't. The result dataframe is:

In [2]: df
Out[2]: 
                     Summary_1 Blade Missing  Summary_Blade in Lockdown
date                                                                   
2016-04-2510:29:000.0NaN2016-04-2613:22:000.0NaN2016-04-2510:10:00NaN1.02016-04-2510:23:00NaN1.0

And the result plot:

enter image description here

I hope this can help you. Regards.

Post a Comment for "Creating Horizontal Bar Plot With Time-series Data In Python"