Beginners Python Cheat Sheet PCC Plotly PDF
Beginners Python Cheat Sheet PCC Plotly PDF
Line graphs, scatter plots, and bar graphs (cont.) Multiple plots
You can include as many data series as you want in a
Making a bar graph visualization. To do this, create one dictionary for each data
# Pass the data and a filename to plot(). from plotly.graph_objs import Scatter
Data visualization involves exploring data through
offline.plot(data, filename='squares.html') from plotly import offline
visual representations. Plotly helps you make visually
appealing representations of the data you’re working
with. Plotly is particularly well suited for visualizations Adding a title and labels x_values = list(range(11))
squares = [x**2 for x in x_values]
that will be presented online, because it supports Using Layout objects cubes = [x**3 for x in x_values]
interactive elements. The Layout class allows you to specify titles, labels, and other
formatting directives for your visualizations. data = [
Installing Plotly from plotly.graph_objs import Scatter, Layout {
from plotly import offline # Trace 1: squares
Plotly runs on all systems, and can be installed in one line.
'type': 'scatter',
Installing Plotly x_values = list(range(11)) 'x': x_values,
squares = [x**2 for x in x_values] 'y': squares,
$ python -m pip install --user plotly 'name': 'Squares',
# Add a title, and a label for each axis. },
Line graphs, scatter plots, and bar graphs data = [Scatter(x=x_values, y=squares)] {
To make a plot with Plotly, you specify the data and then # Trace 2: cubes
pass it to a graph object. The data is stored in a list, so you title = 'Square Numbers' 'type': 'scatter',
can add as much data as you want to any graph. x_axis_config = {'title': 'x'} 'x': x_values,
In offline mode, the output should open automatically in a y_axis_config = {'title': 'Square of x'} 'y': cubes,
browser window. 'name': 'Cubes',
my_layout = Layout(title=title, },
Making a line graph ]
A line graph is a scatter plot where the points are connected. xaxis=x_axis_config, yaxis=y_axis_config)