Why I Use Plotly for Data Visualization | Towards Data Science

The amount of data in the world is growing every second. From sending a text to clicking a link, you are creating data points for companies to use. Insights that can be drawn from this collection of data can be extremely valuable. Every business has their own storage of data that they need to examine. One of the most important ways this examination is done is by visualizing the data.
Simply put — “a picture is worth a thousand words”. In the entire history of business, data visualization has remained a necessary component. The reason it is so necessary is ultimately because we are visual creatures. Why else do you think a majority of us would prefer to watch a movie adaptation of a book than read the book itself? In terms of business presentations, a graph or chart of sales data may prove more insightful than just plain text. It is easy to draw insights from visual mediums rather than word documents.
By visualizing the data you are making the data more accessible to a wider audience. This can help draw more insights because someone else might have an insight or two that you may never have thought of. The more people that see your visualization, then the more insights can potentially be made.
Visualizations also play a key role when presenting to crucial decision makers such as board members or shareholders. As you are constructing your numerous graphs and plots to highlight key data points, the visuals you decide to make can help push these decision makers in one direction or another. If the data visuals are presented with a select narrative in mind, then these decision makers will be inclined to make specific decisions based on your presentation.
Sign up for a Medium Membership here to gain unlimited access and support content like mine! With your support I earn a small portion of the membership fee. Thanks!
Pie charts, bar charts, line graphs, and so on are all effective visuals when presenting data. These visuals are the tried and true forms for data presentation and we have made it even easier to create them. What we once use to do by hand can now be done with a couple of clicks on a computer.
Nowadays, we have access to multiple programs to construct beautiful looking charts and graphs. These tools range from more technically based applications of visualization like Python’s Matplotlib or Plotly to more user-friendly ones like Tableau or Microsoft Power BI. Data visualizations tools are now more accessible than ever before.
Within the realm of Python programming, there are many different libraries you could use to craft data visualizations. These libraries include, but are not limited, to Altair, Seaborn, and Plotly. There is no superior Python library because it all depends on what you are comfortable with and the problem or data you are trying to visualize.
One of the tools we mentioned before is called Plotly. Plotly is a graphing and plotting library in Python similar to Matplotlib. The difference between the two is the fact that Plotly creates dynamically, interactive charts and graphs.
To get started with Plotly, we will need data to graph or plot first. So let’s say for example you work for a business that sells clothing. They want you to chart the sales for their shirts and jeans over the course of one year and have provided you with the data to do so. This problem will help us begin working with Plotly.
In order to begin, we must first install Plotly by using the following command in your terminal:
$ pip install plotly
Or if you have Anaconda installed:
$ conda install -c plotly plotly
Now that you have Plotly installed, let’s open a new file and start importing the necessary libraries for our data visualization example:
import plotly.express as pximport calendar as calimport randomimport pandas as pd
Here we are using plotly.express, which is a module within Plotly that will quickly create graphs and charts for us.
Since we are not actually given real data, we will have to create our own:
data = {'Months': [cal.month_name[i] for i in range(1,13)],        'Shirts': [round(random.gauss(100, 15)) for _ in range(12)],        'Jeans': [round(random.gauss(50, 20)) for _ in range(12)]}
Plotly works very well with Pandas DataFrames so we will store our newly created data into a DF:
df = pd.DataFrame(data)
This new DF looks like this:
Now that we have our DF ready we can begin crafting our bar chart:
fig = px.bar(df,              x='Months',              y=['Shirts','Jeans'])fig.show()
Here we are using the .bar() method and inputting the DF of our data, and specifying the x and y axes. We are crafting a stacked bar chart by making a list for the columns: ‘Shirts’ and ‘Jeans’. Which we’ll display by calling fig.show().
Success! That was simple enough. The cool thing about this Plotly chart is that you can start interacting with it by zooming in, panning, etc. But in regards to the overall chart, there are some things we would like to change to make this graph a little bit more descriptive like adding a title and renaming a few of the labels.
fig = px.bar(df,              x='Months',              y=['Shirts','Jeans'],             title='Total Monthly Item Sales',             labels={'variable': 'Item',                     'value': 'Quantity Sold (in thousands)'})fig.show()
The difference between this code and the code before is the addition of the title= and labels={} argument. With these new arguments we are adding in a title for the chart and under the labels we are basically using a dictionary to replace the two current labels.
Now that the bar chart is properly labeled, we are basically finished with using Plotly for this data. But what if we wanted to do other kinds of charts or graphs in order to view different sides of the data?
Plotly allows us to create other types of visualizations too. We can easily create a line graph by using the code from before and just changing one thing:
fig = px.line(df,               x='Months',               y=['Shirts','Jeans'],              title='Monthly Item Sales',              labels={'variable': 'Item',                      'value': 'Quantity Sold (in thousands)'})fig.show()
All we did here was change px.bar to px.line. This now displays the following:
Now we have a line graph! But wait there’s more...
Let’s say we wanted to compare how many shirts were sold vs how many jeans were sold in the entire year.
First, we must change our data to show the total sum of all sales for shirts and jeans:
pie_df = df[['Shirts','Jeans']].sum()
Here we’re just getting the sum of both Shirts and Jeans from the DF. Then, we will need to use px.pie() using our new summed up DF.
fig = px.pie(values=pie_df.values,              names=pie_df.index,              title="Sales Percentage in a Year")fig.show()
The argument values is used to determine the sizes of each portion of the pie chart. The names are the labels for each of the portions.
Awesome! Now we have created three different types of visualizations for our data. But you don’t have to stop — there are more options available (see here for more) if you feel the need to continue experimenting with Plotly.
After visualizing our data, we would need to come to some sort of insight or conclusion based on the visuals. What can you tell based on these charts? Are there some obvious conclusions that can be drawn? What about some not so obvious ones?
Anyways, insights and conclusions are easier to see rather than read. If you are still wondering about the importance of visualizations, then just take a look back at the DF we created and compare it to any of the visuals we created with Plotly. Sometimes reading information is not as good as seeing the information.