Don't waste your energy with Matplotlib
There is a better package for quick data visualisations.
If you're stepping into the world of Python and data science, you'll quickly appreciate the power of the right tools. Enter Plotly, a nifty library for creating interactive and visually appealing plots, especially for time series data. Let's dive into why Plotly should be your go-to choice for exploring economic and financial data.
Effortless Time Series Analysis
Plotly shines in its ability to handle time series data, a staple in economic and financial analysis. The plot below only requires that you have a an index with ‘datetime’ format and then you can choose the column you wish to see.
column = 'india_arrivals_estimate'
fig = go.Figure(
[go.Scatter(x=df.index, y=df[column], mode="lines", name=column)]
)
fig.update_layout(
title=f"Time Series Plot of {column}",
xaxis_title="Month",
yaxis_title="Value",
template="plotly_dark",
)
fig.show()
With minimal coding, you can generate graphs with some neat time-centric features. For example, sliders and buttons for selecting specific time ranges, making it easier to focus on particular periods (see the documentation here).
The candlestick and faceted area plots are just a few examples of how Plotly caters specifically to financial data, allowing for nuanced and detailed analysis.
Decent Design with Minimal Effort
One of Plotly's strengths is its ability to produce beautiful and professional-looking plots right out of the box. Whether you’re creating box plots, histograms, or scatter plots, the need for extensive formatting, a common hurdle with libraries like Matplotlib, is significantly reduced. You would need 20+ lines of code with Matplotlib to produce a similar scatter plots like the ones below.
fig = px.scatter(df_2007,
x="gdpPercap", y="lifeExp", size="pop", color="continent",
log_x=True, size_max=60,
template=template)
fig.show()
Additionally, Plotly offers a variety of design templates, including a sleek dark mode, plotly_white, and styles inspired by seaborn and ggplot2. This versatility ensures that your visuals are not just informative but also aesthetically pleasing.
Interactive and User-Friendly
Interactivity is a cornerstone of Plotly. Unlike some other libraries where enabling interactive features can be cumbersome (a.k.a Altair!), Plotly is interactive by default. This means you can effortlessly hover over data points to extract key information, making your data exploration both intuitive and detailed.
There is also the possibility to share plots to the wider public via Chart Studio (see here for a guide). This interactivity enhances the user experience, making your analyses more accessible.
Don’t get me wrong; there is a time and place for Matplotlib. For instance, it's ideal when someone needs a really customized design, and I will share some possibilities in future posts. However, as a day-to-day tool, Plotly stands out due to its ease of use, beautiful default designs, and interactive capabilities, making it an invaluable asset for Analysts new to Python.
Completely agree, I love Plotly! Particularly when combined with Streamlit. Plotly has the best balance of ease of use, functionality, flexibility and interactivity (that we Data Analysts like, but most C-Suite types just want a static chart!).