Visualizing Equity Data with a Bubble Chart

Apr 1, 2023

2 min read


programmer-with-chrome

What Idea about bubble chart ?


A bubble chart is an effective way to visualize equity data, allowing us to perform a top-down analysis of various factors such as market capitalization, Return on Equity (ROE), and Price-to-Book Value (P/BV). In this tutorial, we will walk through the process of creating a bubble chart using Python’s pandas and matplotlib libraries.


Step 1: Import pandas and matplotlib Start by importing the necessary libraries:


import pandas as pd
import matplotlib.pyplot as plt

Step 2: Import Dataframe from Excel Read the data from an Excel file and store it in a dataframe:


data = pd.read_excel('Data.xlsx',engine ='openpyxl')
data

Here’s what the dataframe should look like:


StocksROEPBVMARKETCAP
0SAWAD22.563.8786851
1MTC26.435.57125610
2TIDLOR13.343.8780584
3AMANAH21.023.315230
4AEONTS26.362.5245750
5ASK14.422.1618606
6CHAYO9.834.8811444
7JMT20.866.5951346
8KTC26.355.91143097
9MICRO11.543.676872
10MTC26.435.57125610
11NCAP19.815.109360
12S1114.611.384045
13SAK11.863.8317396
14THANI19.342.1322765
15TK8.190.864600

Step 3: Create the Bubble Chart Use the matplotlib library to generate a scatter plot with custom bubble sizes:


fig, ax = plt.subplots()
for i, stock in data.iterrows():
ax.scatter(stock.ROE, stock.PBV, s = stock.MARKETCAP/100, alpha=0.5)
ax.annotate(stock.Stocks, (stock.ROE, stock.PBV))

plt.title('Bubble Chart')
plt.xlabel('ROE')
plt.ylabel('P/BV')
plt.xlim(0,30)
plt.ylim(0,8)
plt.show()

Explanation of the code:


Create a scatter plot using plt.subplots().


Iterate through the dataframe and plot each data point on the chart using ax.scatter().


Set the size of the bubbles using the ‘s’ parameter and the market capitalization divided by 100 (s=stock.MARKETCAP/100).


Use alpha=0.5 to make the bubbles semi-transparent.


Add data labels to the bubbles using ax.annotate().


Buble-Chart


Conclusion


In this tutorial, we demonstrated how to create a bubble chart to visualize equity data using Python’s pandas and matplotlib libraries. This technique allows us to display various factors, such as ROE and P/BV, on the x and y axes, while the bubble size represents market capitalization. You can customize this chart further by choosing different data points for the axes, depending on your specific analysis needs.