Posit has announced the 2024 plotting contest and here is my submission.
As you may know, only 3 companies have reached the 3T$ of market cap milestone: Apple, Microsoft and NVIDIA. But, how much time did they take to reach that milestone? Let’s check it using polars and plotnine!
Let’s import our dependencies, just polars and plotnine (and pathlib)
We define a function that we’ll use later. In the chart, we want to show some segments connecting the moments when each company reached the 3T$ milestone.
For that, we need to know, for each company, the minimum date where its market cap is higher than the milestone (is parametrized, but it will be 3T$ in this case).
At this point we should have a data frame with 3 rows, one per company. Using a cross join (a cartesian product), we can get all the combinations among companies, but we only want 3 of them. Those where the first date is lower than the second one (for example, if NVIDIA reached 3T$ in less time than Apple, we want that combination).
Finally, we can define the label that we want to show in the chart and where we want to show it (X and Y axis).
We set some constants, like the data path and the milestone itself (we could change it to 1T$ or 2T$!)
This is the data that we’ll use, coming from Kaggle.
The first dataset (df_companies), contains information about S&P companies. We'll use it to:
Get the company name from the ticket
Calculate the number of shares based on the current market cap and its price
The dataset has been downloaded from Kaggle: link
The second dataset contains historical stock prices from Apple, Microsoft and NVIDIA. We'll keep the ticket, the adjusted closed price for a specific date, and the date itself.
The original data sources have been downloaded from Kaggle:
And finally, we can join, preprocess the data and plot it! Each step contains a comment showing what it’s doing, but at high order we:
Join both datasets to calculate the market cap of each company for each date
Calculate a column called rank, which will represent the time since the OPA of each company
Plot it! For each company:
geom_line to show the market cap evolution
geom_label containing the name
geom_segment to plot lines between the milestones (using the function that we defined above)
geom_text to show the labels above the segment
geom_vline to show vertical lines in the 3T$ milestone
Finally, change scales, axis, title, theme… Fancy chart!
And here’s the result!
You can find the code in my GitHub