Plotly’s Python graphing library makes interactive, publication-quality graphs.
See https://plotly.com/python/
Plotly’s Express data
module has some built-in datasets for practicing on.
-
Plotly’s Figure
object.
-
Plotly’s graph_objects
module.
Plotly is a data visualisation system that has over 50 different types of charts, all of which can be highly customised.
Plotly’s Figure
object has two main attributes, the data
and layout
attributes.
The layout of the plot consists of
-
data
: the different attributes of data and relationships between them are expressed using graphical shapes, the size of and distance between the shapes tell us about the various attributes of the data. The values to be provided for the data
attribute depends on the chart type.
Several layers of data can be overlaid on each other in a chart. Each one is called a trace.
-
layout
: everything else around the data including styling elements, titles, legends, axis titles, ticks etc.
import plotly.graph_objects as go
.
An empty Figure object is instantiated by calling go.Figure
. Everything can be defined within the go.Figure()
call but an easier way is to assign the created Figure
object to a variable.
fig = go.Figure()
This variable iteratively be added to and modified. This method allows you to make changes to the chart after creating it.
A Figure
object assigned to a variable will become available in the global scope and it is mutable, the variable can be changed elsewhere in the code.
There are many methods for manipulating the Figure
object after it has been assigned to a variable. Such method that are for adding data traces to the object begin with add_
, for example add_bar
or add_scatter
.
To display the figure fig
by just calling fig
or by explicitly calling the show
method. fig.show()
fig.add_scatter(x=[], y =[])
import plotly.graph_objects as go
# create an instance of a Figure object and assign to a variable
fig = go.Figure()
# add a scatter trace
fig.add_scatter(x=[2,4,6], y = [12,34,53])
# show the resulting figure
fig.show()
# add another trace
fig.add_scatter(x=[10,110,15], y =[2,5,9])
- to modify any aspect of the
data
trace use add_<chart type>
- to modify anything related to the
layout
access and assign the attributes declaratively using dot notation. For example figure.attribute.sub_attribute = value
.
fig.layout.title = 'A title for the Figure'
fig.layout.xaxis.title = 'a title for the X-axis'
fig.layout.yaxis.title = 'a title for the y-axis'
New traces added to a figure will be given default colours and legends.
Responsive plots
Plotly figures are responsive. The axis tick names will be displayed vertically or diagonally at an angle depending on the browser window size.
Theming figures
See https://plotly.com/python/templates/
Figures can also be themed by modifying the template
attribute under layout
. The full list of themes is available under plotly.io.templates
.
fig.layout.template = template_name
Templates include seaborn
, plotly_dark
, ggplot2
and more.
import plotly.io as pio
pio.templates
Templates configuration
-----------------------
Default template: 'plotly'
Available templates:
['ggplot2', 'seaborn', 'simple_white', 'plotly',
'plotly_white', 'plotly_dark', 'presentation', 'xgridoff',
'ygridoff', 'gridon', 'none']
Plotly Express
import plotly.express as px
Instead of starting with an empty rectangle and building layers on top of it, you start with the columns or features of the dataset and create visualisations based on them.
Plotly’s Python graphing library makes interactive, publication-quality graphs.
See https://plotly.com/python/
Plotly’s Express data
module has some built-in datasets for practicing on.
-
Plotly’s
Figure
object. -
Plotly’s
graph_objects
module.
Plotly is a data visualisation system that has over 50 different types of charts, all of which can be highly customised.
Plotly’s Figure
object has two main attributes, the data
and layout
attributes.
The layout of the plot consists of
-
data
: the different attributes of data and relationships between them are expressed using graphical shapes, the size of and distance between the shapes tell us about the various attributes of the data. The values to be provided for thedata
attribute depends on the chart type. Several layers of data can be overlaid on each other in a chart. Each one is called a trace. -
layout
: everything else around the data including styling elements, titles, legends, axis titles, ticks etc.
import plotly.graph_objects as go
.
An empty Figure object is instantiated by calling go.Figure
. Everything can be defined within the go.Figure()
call but an easier way is to assign the created Figure
object to a variable.
fig = go.Figure()
This variable iteratively be added to and modified. This method allows you to make changes to the chart after creating it.
A Figure
object assigned to a variable will become available in the global scope and it is mutable, the variable can be changed elsewhere in the code.
There are many methods for manipulating the Figure
object after it has been assigned to a variable. Such method that are for adding data traces to the object begin with add_
, for example add_bar
or add_scatter
.
To display the figure fig
by just calling fig
or by explicitly calling the show
method. fig.show()
fig.add_scatter(x=[], y =[])
import plotly.graph_objects as go
# create an instance of a Figure object and assign to a variable
fig = go.Figure()
# add a scatter trace
fig.add_scatter(x=[2,4,6], y = [12,34,53])
# show the resulting figure
fig.show()
# add another trace
fig.add_scatter(x=[10,110,15], y =[2,5,9])
- to modify any aspect of the
data
trace useadd_<chart type>
- to modify anything related to the
layout
access and assign the attributes declaratively using dot notation. For examplefigure.attribute.sub_attribute = value
.
fig.layout.title = 'A title for the Figure'
fig.layout.xaxis.title = 'a title for the X-axis'
fig.layout.yaxis.title = 'a title for the y-axis'
New traces added to a figure will be given default colours and legends.
Responsive plots
Plotly figures are responsive. The axis tick names will be displayed vertically or diagonally at an angle depending on the browser window size.
Theming figures
See https://plotly.com/python/templates/
Figures can also be themed by modifying the template
attribute under layout
. The full list of themes is available under plotly.io.templates
.
fig.layout.template = template_name
Templates include seaborn
, plotly_dark
, ggplot2
and more.
import plotly.io as pio
pio.templates
Templates configuration
-----------------------
Default template: 'plotly'
Available templates:
['ggplot2', 'seaborn', 'simple_white', 'plotly',
'plotly_white', 'plotly_dark', 'presentation', 'xgridoff',
'ygridoff', 'gridon', 'none']
Plotly Express
import plotly.express as px
Instead of starting with an empty rectangle and building layers on top of it, you start with the columns or features of the dataset and create visualisations based on them.