silverpegasus
silverpegasus

Reputation: 101

Formating a frequency dataframe to a table and a histogram

If I have a frequency table like below:

             ret < -1(%)  -1 < ret < -0.5(%)  -0.5 < ret < 0(%)  0 < ret < 0.5(%)  0.5 < ret < 1(%)  ret > 1(%)
# of event   185.000000           39.000000           54.00000         39.000000         41.000000  188.000000
% of event    33.882784            7.142857            9.89011          7.142857          7.509158   34.432234

which is generated from the code:

regions = pd.cut(portretDF['Daily Return'],
                 bins=[-np.inf, -0.01, -0.005, 0, 0.005, 0.01, np.inf],
                 labels=['ret < -1(%)','-1 < ret < -0.5(%)', '-0.5 < ret < 0(%)','0 < ret < 0.5(%)','0.5 < ret < 1(%)','ret > 1(%)'],
                 )
    count = regions.value_counts(sort=False)
    print(count)
    
    size = len(regions)
    percentage = (count/size) * 100
    print(percentage)
    
    results = pd.DataFrame({'# of event': count, '% of event': percentage})

    print(results.T.to_string())

where print(count) and print(percentage) are

ret < -1(%)           185
-1 < ret < -0.5(%)     39
-0.5 < ret < 0(%)      54
0 < ret < 0.5(%)       39
0.5 < ret < 1(%)       41

and

ret < -1(%)           33.882784
-1 < ret < -0.5(%)     7.142857
-0.5 < ret < 0(%)      9.890110
0 < ret < 0.5(%)       7.142857
0.5 < ret < 1(%)       7.509158
ret > 1(%)            34.432234
ret > 1(%)            188

respectively.

May I ask how can I generate a formatted table and histogram with the format like below? enter image description here

Upvotes: 0

Views: 419

Answers (1)

AJH
AJH

Reputation: 799

Here's a way to use seaborn for the graph and regular matplotlib for the table. My way assumes that # of events and % of events are renamed indices for the df.

import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.axes import Axes
import pandas as pd

# Get necessary values from df
column_names = df.columns.values.tolist()
counts = df.loc["# of event"].tolist()
percents = df.loc["% of event"].tolist()
row_names = df.index.tolist()

# Make bar plot with seaborn
sns.set()
ax = sns.barplot(x = column_names, y = counts, color = "cornflowerblue")

# Set axis labels and rotate x labels
Axes.set(ax, xlabel = None, ylabel = "Counts")
ax.set_xticklabels(ax.get_xticklabels(), rotation = 45, ha='right', rotation_mode='anchor')

# Make table with matplotlib. Change the number in '%.4f' to chance number of decimals.
table = plt.table(cellText = [['%.4f' % elem for elem in counts], ['%.4f' % elem for elem in percents]],
          rowLabels = row_names,
          rowColours = ["#A7C6F9"]*2,
          colLabels = column_names,
          colColours = ["#A7C6F9"]*len(column_names),
          bbox=(-0.2, 1.1, 1.6, 0.3))

table.auto_set_font_size(False)
table.set_fontsize(9)

The above results in this: Table and bar graph

Upvotes: 1

Related Questions