Interactive Stock Data Visualization with Plotly vs. Matplotlib in Python
This tutorial compares static Matplotlib/Seaborn visualizations with interactive Plotly charts for stock price data, explains when to choose each library, and provides step‑by‑step Python code for fetching data, creating scatter and 3D surface plots, and installing Plotly in offline mode.
When learning Python for machine learning, most people start with Matplotlib and Seaborn because they have a low learning curve, simple syntax, and are easy to use, but the resulting charts are static images without interactivity.
Plotly, on the other hand, offers zooming, panning, hover information, and the ability to embed the chart in HTML, making it suitable for interactive visualizations.
The example uses stock data downloaded via pandas_datareader from Yahoo Finance, retrieving daily high prices for Alibaba (BABA), Apple (AAPL), Microsoft (MSFT), and IBM.
<code>import pandas_datareader as pdr
import numpy as np
import pandas as pd</code> <code>Stock_list = ['BABA', 'AAPL', 'MSFT', 'IBM']
Data = [pdr.get_data_yahoo(i)['High'] for i in Stock_list]
Data_month = [i.resample('1m').mean() for i in Data]
result_df = pd.concat(Data_month, axis = 1)
result_df.columns = Stock_list</code>Using Matplotlib, the DataFrame can be plotted directly with result_df.plot() , producing a static line chart.
<code>result_df.plot()</code>Plotly creates an interactive version of the same data; the GIF in the original article demonstrates the interactivity.
When to use each library: Matplotlib is sufficient for model‑evaluation plots such as checking over‑fitting or ROC curves, while Plotly is better for reporting dashboards where users need to explore details.
Compared with ECharts (a Java‑based library), Plotly can be used entirely in Python, requiring less code and no Java knowledge.
Installation of Plotly is done via pip, preferably using the Tsinghua mirror for faster downloads, and the offline mode is recommended to avoid cloud‑storage limits.
<code>pip install -i https://pypi.tuna.tsinghua.edu.cn/simple plotly</code>To use Plotly offline in a notebook:
<code>from plotly import tools
import plotly.offline as py
py.offline.init_notebook_mode()
import plotly.graph_objs as go
import plotly.figure_factory as ff
config = {'showLink': False}</code>Example of a scatter plot for IBM stock data:
<code>IBM = result_df['IBM']
data = [go.Scatter(x = IBM.index,
y = IBM.values,
mode = 'markers',
name = 'IBM')]
layout = go.Layout(title = 'plotly graph for stock data',
xaxis = {'title' : 'Date'},
yaxis = {'title' : 'High'},
template = 'plotly_white')
fig = go.Figure(data = data, layout = layout)
fig.show()</code>Changing the mode to 'lines+markers' connects the points with lines.
<code>data = [go.Scatter(x = IBM.index,
y = IBM.values,
mode = 'lines+markers',
name = 'IBM')]
# layout unchanged
fig = go.Figure(data = data, layout = layout)
fig.show()</code>Multiple stocks can be plotted by adding additional go.Scatter objects to the data list.
<code>AAPL = result_df['AAPL']
data = [go.Scatter(x = IBM.index, y = IBM.values, mode = 'lines+markers', name = 'IBM'),
go.Scatter(x = AAPL.index, y = AAPL.values, mode = 'lines+markers', name = 'AAPL')]
fig = go.Figure(data = data, layout = layout)
fig.show()</code>For many stocks, a list comprehension can generate the Scatter objects automatically.
<code>data = [go.Scatter(x = result_df.index,
y = result_df[i].values,
mode='lines+markers',
name=i) for i in result_df.columns]
fig = go.Figure(data = data, layout = layout)
fig.show()</code>The article also mentions Plotly’s 3D‑surface plots, which are useful for visualizing concepts such as gradient descent in machine learning; an example from the official Plotly site is referenced.
Overall, Plotly provides interactive, web‑embeddable visualizations that complement the static charts produced by Matplotlib, making it a valuable tool for both exploratory data analysis and presentation.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.