Creating an Interactive Financial Chart in Python
Creating an interactive financial chart is going to be much easier than you thought possible. It can be done in only a few lines of code and will be a great tool to explore your data. The solution is simple and it’s probably because you are not using the correct library.
This library works in Jupyter Notebook, not Jupyter Lab, so if you are not seeing your graph, switch over to Jupyter Notebook.
First, install plotly. Plotly is a graphing library like seaborn and matplot.
For this example, I’ll be showing you AAPL data. If you d not already have AAPL data, you can go to yahoo finance and download a csv file of their historical data.
Read in your AAPL.csv and look at the column names. We will want to extract the date column and adjusted close column. My chart will look different than yours, but the result will be the same.
You’ll notice df.tail() gives me the most recent data. This is because when you pull the historical data, the first entry is the oldest date, and the latest entries are the more recent ones.
On our chart, we want our x-axis to be the date, and the y-axis to be our adjusted close price. To do that we will run the following
Now that we have our axes, we can graph our chart.
The resulting graph looks like this:
But what if you have multiple csv files of stock you want to look at and you do not want to do this every time to look at one graph? We can make that happen very quick and easy.
Using the input() function, we can enter the name of the stock we have and pull the chart. Make sure your x and y columns are correctly labled, as I have changed my chart names around. The fig.update_xaxes option lets you add the 10 day, 1 month, 6 month, and 1 year option. This is not necessary for the graph, but it is interesting to see how the chart moves in those time periods.
I highly recommend using plotly for your financial data as it is easier to see pricing and to select the range of dates you want to look at.