Basic Thoughts while working with Plotly for Python
I recently worked with Plotly for data visualization on predicted outputs coming from a Machine Learning Model.
The documentation I referred to : https://plotly.com/python/
Here are few “google searches” I personally did while I was working on it:
Question : Which data viz library should I choose for interactive plots?Answer : Matplotlib, Seaborn, Plotly, Altair, and Bokeh were some of the answers. The reason I went with Plotly was because my project requirement was to get charts on a html page which is supported by Plotly. With Plotly, the charts are stored as .html files and can be zoomed in, zoomed out and also focused on a particular region.
Question: Plotly Express or Plotly Graph Objects
Answer : I started with Plotly Express but ended up using Plotly Graph Objects for the very reason that it provided me a lot of modifications to my existing code. There were more number of attributes which could provide better cosmetic changes to the charts.
In terms of Plotly Graph Objects
import plotly.graph_objs as go
Question: How do I show two columns from my data set against time?
Answer : choose your “mode” as lines and lines + markers.
mode = ‘lines’ , can be set when you add your trace.
fig = go.Figure()
fig.add_trace(go.Scatter(
x=data[‘time’],
y=data[‘Values_x’],
mode=‘lines’))
fig.add_trace(go.Scatter(
x=data[‘time’],
y=data[‘Values_y’],
mode=‘lines+markers’))
Question: What else I can add in my go.Scatter() function to modify my data points?
Answer: Other attributes which can be used to modify the plots can be
name : name of the data point which is being plotted, string value
showlegend : If the legend should be visible or not, Boolean value with True or False
marker_color : if your mode is lines+markers or just markers you can give a specific color to it, it accepts RGBA, RGB, HSL, or a name of the color in a string value
line_width : determines the width of the line in your plot, accepts an int value
line_color : the color of the line
font : one can choose the font from the font family available in plotly.
Question: Once I choose my font family, how do I choose the font color and its size?
Answer: Many attributes in plotly have an option of specifying a dict and further writing a key value pair in it.
font=dict(family=’Times New Roman’,size=16,color=’red’)
Question: How do I put a hover label on my plots?
Answer : Hover labels are kind of boxes which are visible when you move your cursor to a specific data point. They help the user in understanding the value and other details in your chart.
hoverlabel=dict(bgcolor=’lightblue’,font=dict(family=’Times New Roman’,size=16),bordercolor=’black’),hovertemplate=’Booth Humidity<br>Probability: %{y}’)
bgcolor is the back ground color of the hover box
hovertemplate will contain basic html tags, it can be altered according to the style one wants to keep.
Question: How do I make subplots in plotly?
Answer: There are various kinds of subplots which we can make on plotly by specifying the number of rows and columns.
fig.make_subplots(rows=3,column=1,vertical_spacing=0.5,horizontal_spacing=0.5)
This will give you 3 subplots stacked together. It can be varied accordingly.
Vertical spacing indicates the distance between the columns of the subplots specified whereas the Horizontal Spacing is between two rows of the subplots.
Simply specify row and column number in each go.Scatter function. This way you can also keep multiple data points in the same subplot. (i.e two or more go.Scatter can have same row number and column number)
Question: How do I make one subplot to be larger in size than my other two subplots?
Answer: There is a very useful parameter called “specs” which is basically a 2D list inside the make_subplots() function where one can specify the colspan, rowspan or None.
None indicates that no subplot will be drawn in that dimension and hence that is where your larger sized subplot goes.
Question: Can I make a subplot with common axis?
Answer: Plotly has a provision of making subplots with shared (common) xaxis and yaxis.
shared_xaxes or shared_yaxes has to be set to true in the make_subplots() function
Question: How do I give different xaxis title or yaxis title to each subplot?
Answer: you can specify the axis title with the subplot number in the update_layout() function.
Example- for subplot in the row 2 has the yaxis titled as yaxis2_title
Question: What is update_layout and what all attributes it holds?
Answer: update_layout is the overall function to make the plot look presentable. One can specify the following in it:
height : Height of the chart
width : Width of the chart
title: The overall title of your chart
showgrid: the horizontal and vertical gridlines present in your chart
plot_bgcolor: the color inside the plot
paper_bgcolor: the color where the plot is present
Question: Does Plotly have individual functions to update xaxis and yaxis?
Answer: It indeed does, update_xaxes and update_yaxes has attributes like showgrid, title_font, etc which can be modified as per the requirements.
Question: Can we give names to each subplot?
Answers: Each subplot can be given a title by the attribute subplot_titles present in the make_subplots() function.
I have also used the other functionalities like a Range Slider and buttons in Plotly which I will be discussing in my next article very soon!
This was the first time I touched upon Data Visualization using Python. I have also written few other articles on Data Viz using tools like PowerBI which can be found here
I have also developed an alerting system app using PowerApps and written an article about an awesome function I used there to integrate it with my PowerBI reports. Do check that out too here.
Let me know if you have any queries or suggestions regarding this by commenting below or reach me out on Twitter for any fun discussions revolving around Data Viz or Pandas! :)