When you start out your data analytics journey with python and when you get to the "data visualization" part, most likely you will be introduced to matplotlib, the widely famous and highly capable python plotting library or its younger (and slightly prettier 🤏) relative, seaborn!
Those are GREAT 🤩 tools! You can create almost all kind of plots known to mankind with them 😁. Not just that, but they also offer the ability to highly customize your graphs to be both efficient and good looking in a relatively easy way.
But let's face it! We are living in the age of AI, ML, AR, VR and all those cool "two-letters technologies" 😅 and it just doesn't feel right that modern data analytics graphs are static pictures, right? 🤔
Plotly is a set of "Open Source Graphing Libraries" available not only for python but to a variety of other languages as well like R, Javasript, Matlab, ..., etc. And the cool thing about Plotly is that it can do all the things that the previously mentioned libraries do, and it is Interactive 🤩
Here we will focus on Plotly for Python and in this post I'll try to show you how to get up and running with Plotly ... fast. 😉
Plotly has two ways of how you can create graphs, the fast and easy way a.k.a. Plotly Graph Objects and the also fast but easier way a.k.a. Plotly Express.
the Graph Objects is the low-level way to deal with Plotly but don't be intimidated by that and assume immediately that this is complex method to create plots. Absolutely not! This method is very simple that I almost always use it whenever I use Plotly 🙂 and here I'll focus more on this method.
You can install Plotly the usual way by using pip
pip install plotly
To use Plotly, you need to import either the graph_objects or express modules to start plotting.
import plotly.graph_objects as go # go is a plotly import convention import plotly.express as px # px is a convention for plotly express
With the graph objects approach, you will build your "Figure" by supplying your trace(s) (plot) and layout(s) to the Figure class. But personally, I prefer to initiate the figure first then add those as I go.
fig = go.Figure() # Initiating an empty Figure instance.
You can think of the figure as the canvas where you will be placing your trace(s) (plot(s)) and customizing them with the layout in which you can control things like the main title, axis title, ticks values and formats, ..., etc.
To add a trace to the figure, you only have to call it from the go module and pass it to the figure's add_trace method:
fig.add_trace( go.Bar( name="This is the trace name", x=["red", "blue", "yellow", "orange", "green"], y=[3, 5, 7, 2, 9] ) )
And finally to show the figure if you are working in a Jupyter Notebook, use the show method:
et voila, just like that you will have the following plot! See how easy and fast it is!
Now let's make this plot a little bit prettier! 🙂 We can do that using the layout. We can "update" the layout of the initiated Figure instance using the update_layout method as follows:
fig.update_layout( title="M&Ms count", # Set the plot name. Note this is different than the trace name. xaxis=dict(title="M&M Name"), # Set x-axis properties yaxis=dict(title="Count"), # Set y-axis properties autosize=False, # Disable the autosizing so we can change the plot dimenssions manually. width=500, # Set the width. height=500 # Set the height. ) fig.show()
Much better! Right? 😉
Up until now there is virtually no difference between Plotly and Matplotlib and Seaborn but what makes Plotly way cooler is its interactive plots!
Not only that! By default, every plot comes with its own control menu which makes interactions more easier:
There are a lot you can do with Ploty!
Ever saw those cool 2-D aninmated scatter plots that changes over time? Plotly got you covered.
Need to create 3D plots? No problem! 😉
Plotly literally gives you a canvas to let lose your data analytics artistic tendencies 🎨🖼️.
I've created a notebook in this Github repo to demo some of Plotly's basic capabilities that I couldn't fit in here. And I highly recommend checking out the official documentations for examples of the different plot types that Plotly can produce and to discover lots of other cool stuff that you can do too 🙂.