@@ -35,61 +35,58 @@ jupyter:
35
35
redirect_from :python/line-and-scatter-plots-tutorial/
36
36
thumbnail :thumbnail/line-and-scatter.jpg
37
37
title :Python Scatter Plots | plotly
38
+ v4upgrade :true
38
39
---
39
40
40
41
##Scatter plot with plotly express
41
42
42
43
Plotly express functions take as argument a tidy[ pandas DataFrame] ( https://pandas.pydata.org/pandas-docs/stable/getting_started/10min.html ) . With`` px.scatter `` , each data point is represented as a marker point, which location is given by the` x ` and` y ` columns.
43
44
44
45
``` python
45
- import plotly_express as px
46
+ import plotly.express as px
46
47
iris= px.data.iris()
47
48
fig= px.scatter(iris,x = " sepal_width" ,y = " sepal_length" )
48
49
fig.show()
49
50
```
50
51
51
52
####Set size and color with column names
52
53
53
- Note that` color ` and` info ` data are added to hover information. You can add other columns to hover data with the` hover_data ` argument of` px.scatter ` .
54
+ Note that` color ` and` size ` data are added to hover information. You can add other columns to hover data with the` hover_data ` argument of` px.scatter ` .
54
55
55
56
``` python
56
- import plotly_express as px
57
+ import plotly.express as px
57
58
iris= px.data.iris()
58
59
fig= px.scatter(iris,x = " sepal_width" ,y = " sepal_length" ,color = " species" ,
59
- size = ' petal_length' )
60
+ size = ' petal_length' , hover_data = [ ' petal_width ' ] )
60
61
fig.show()
61
62
```
62
63
63
64
##Line plot with plotly express
64
65
65
66
``` python
66
- import plotly_express as px
67
+ import plotly.express as px
67
68
gapminder= px.data.gapminder().query(" continent == 'Oceania'" )
68
- px.line(gapminder,x = ' year' ,y = ' lifeExp' ,color = ' country' )
69
+ fig= px.line(gapminder,x = ' year' ,y = ' lifeExp' ,color = ' country' )
70
+ fig.show()
69
71
```
70
72
71
73
##Scatter and line plot with go.Scatter
72
74
73
- When data are not available as tidy dataframes, it is possible to use the more generic` go.Scatter ` function from` plotly.graph_objs ` . Whereas` plotly_express ` has two functions` scatter ` and` line ` ,` go.Scatter ` can be used both for plotting points (makers) or lines, depending on the value of` mode ` . The different options of` go.Scatter ` are documented in its[ reference page] ( https://plot.ly/python/reference/#scatter ) .
75
+ When data are not available as tidy dataframes, it is possible to use the more generic` go.Scatter ` function from` plotly.graph_objects ` . Whereas` plotly.express ` has two functions` scatter ` and` line ` ,` go.Scatter ` can be used both for plotting points (makers) or lines, depending on the value of` mode ` . The different options of` go.Scatter ` are documented in its[ reference page] ( https://plot.ly/python/reference/#scatter ) .
74
76
75
77
76
78
####Simple Scatter Plot
77
79
78
80
``` python
79
- import plotly.graph_objsas go
80
-
81
- # Create random data with numpy
81
+ import plotly.graph_objectsas go
82
82
import numpyas np
83
83
84
84
N= 1000
85
- random_x = np.random.randn(N )
86
- random_y = np.random.randn(N )
85
+ t = np.linspace( 0 , 10 , 100 )
86
+ y = np.sin(t )
87
87
88
- # Create a trace
89
- trace= go.Scatter(x = random_x,y = random_y,
90
- mode = ' markers' )
88
+ fig= go.Figure(data = go.Scatter(x = t,y = y,mode = ' markers' ))
91
89
92
- fig= go.Figure(data = [trace])
93
90
fig.show()
94
91
```
95
92
@@ -98,7 +95,7 @@ fig.show()
98
95
Use` mode ` argument to choose between markers, lines, or a combination of both. For more options about line plots, see also the[ line charts notebook] ( https://plot.ly/python/line-charts/ ) and the[ filled area plots notebook] ( https://plot.ly/python/filled-area-plots/ ) .
99
96
100
97
``` python
101
- import plotly.graph_objs as go
98
+ import plotly.graph_objects as go
102
99
103
100
# Create random data with numpy
104
101
import numpyas np
@@ -109,18 +106,19 @@ random_y0 = np.random.randn(N) + 5
109
106
random_y1= np.random.randn(N)
110
107
random_y2= np.random.randn(N)- 5
111
108
112
- # Create traces
113
- trace0= go.Scatter(x = random_x,y = random_y0,
109
+ fig= go.Figure()
110
+
111
+ # Add traces
112
+ fig.add_trace(go.Scatter(x = random_x,y = random_y0,
114
113
mode = ' markers' ,
115
- name = ' markers' )
116
- trace1 = go.Scatter(x = random_x,y = random_y1,
114
+ name = ' markers' ))
115
+ fig.add_trace( go.Scatter(x = random_x,y = random_y1,
117
116
mode = ' lines+markers' ,
118
- name = ' lines+markers' )
119
- trace2 = go.Scatter(x = random_x,y = random_y2,
117
+ name = ' lines+markers' ))
118
+ fig.add_trace( go.Scatter(x = random_x,y = random_y2,
120
119
mode = ' lines' ,
121
- name = ' lines' )
120
+ name = ' lines' ))
122
121
123
- fig= go.Figure(data = [trace0, trace1, trace2])
124
122
fig.show()
125
123
```
126
124
@@ -129,98 +127,77 @@ fig.show()
129
127
In[ bubble charts] ( https://en.wikipedia.org/wiki/Bubble_chart ) , a third dimension of the data is shown through the size of markers. For more examples, see the[ bubble chart notebook] ( https://plot.ly/python/bubble-charts/ )
130
128
131
129
``` python
132
- import plotly.graph_objs as go
130
+ import plotly.graph_objects as go
133
131
134
- trace0 = go.Scatter(
132
+ fig = go.Figure( data = go.Scatter(
135
133
x = [1 ,2 ,3 ,4 ],
136
134
y = [10 ,11 ,12 ,13 ],
137
135
mode = ' markers' ,
138
136
marker = dict (size = [40 ,60 ,80 ,100 ],
139
137
color = [0 ,1 ,2 ,3 ])
140
- )
138
+ ))
141
139
142
- fig= go.Figure(data = [trace0])
143
140
fig.show()
144
141
```
145
142
146
143
####Style Scatter Plots
147
144
148
145
``` python
149
- import plotly.graph_objs as go
146
+ import plotly.graph_objects as go
150
147
import numpyas np
151
148
152
- N= 500
153
149
154
- trace0= go.Scatter(
155
- x = np.random.randn(N),y = np.random.randn(N)+ 2 ,
156
- name = ' Above' ,
157
- mode = ' markers' ,
158
- marker = dict (
159
- size = 10 ,
160
- color = ' rgba(152, 0, 0, .8)' ,
161
- line = dict (width = 2 ,color = ' rgb(0, 0, 0)' )
162
- )
163
- )
150
+ t= np.linspace(0 ,10 ,100 )
151
+
152
+ fig= go.Figure()
164
153
165
- trace1= go.Scatter(
166
- x = np.random.randn(N),
167
- y = np.random.randn(N)- 2 ,
168
- name = ' Below' ,
154
+ fig.add_trace(go.Scatter(
155
+ x = t,y = np.sin(t),
156
+ name = ' sin' ,
169
157
mode = ' markers' ,
170
- marker = dict (size = 10 ,color = ' rgba(255, 182, 193, .9)' ,line_width = 2 )
171
- )
158
+ marker_color = ' rgba(152, 0, 0, .8)'
159
+ ))
160
+
161
+ fig.add_trace(go.Scatter(
162
+ x = t,y = np.cos(t),
163
+ name = ' cos' ,
164
+ marker_color = ' rgba(255, 182, 193, .9)'
165
+ ))
166
+
167
+ # Set options common to all traces with fig.update_traces
168
+ fig.update_traces(mode = ' markers' ,marker_line_width = 2 ,marker_size = 10 )
169
+ fig.update_layout(title = ' Styled Scatter' ,
170
+ yaxis_zeroline = False ,xaxis_zeroline = False )
172
171
173
- layout= go.Layout(title = ' Styled Scatter' ,
174
- yaxis_zeroline = False ,xaxis_zeroline = False )
175
172
176
- fig= go.Figure(data = [trace0, trace1],layout = layout)
177
173
fig.show()
178
174
```
179
175
180
176
####Data Labels on Hover
181
177
182
178
``` python
183
- import plotly.graph_objsas go
184
- import random
185
- import numpyas np
179
+ import plotly.graph_objectsas go
186
180
import pandasas pd
187
181
188
- l= []
189
- y= []
190
182
data= pd.read_csv(" https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv" )
191
- # Setting colors for plot.
192
- N= 53
193
- c= [' hsl(' + str (h)+ ' ,50%' + ' ,50%)' for hin np.linspace(0 ,360 , N)]
194
-
195
- for iin range (int (N)):
196
- y.append((2000 + i))
197
- trace0= go.Scatter(
198
- x = data[' Rank' ],
199
- y = data[' Population' ]+ (i* 1000000 ),
200
- mode = ' markers' ,
201
- marker = dict (size = 14 ,line_width = 1 ,color = c[i],opacity = 0.3 ),
202
- name = y[i],
203
- text = data[' State' ])# The hover text goes here...
204
- l.append(trace0);
205
-
206
- layout= go.Layout(
207
- title = ' Stats of USA States' ,
208
- hovermode = ' closest' ,
209
- xaxis = dict (title = ' Population' ,zeroline = False ,gridwidth = 2 ),
210
- yaxis = dict (title = ' Rank' ,gridwidth = 2 ),
211
- showlegend = False
212
- )
213
- fig= go.Figure(data = l,layout = layout)
183
+
184
+ fig= go.Figure(data = go.Scatter(x = data[' Postal' ],
185
+ y = data[' Population' ],
186
+ mode = ' markers' ,
187
+ marker_color = data[' Population' ],
188
+ text = data[' State' ]))# hover text goes here
189
+
190
+ fig.update_layout(title = ' Population of USA States' )
214
191
fig.show()
215
192
```
216
193
217
194
####Scatter with a Color Dimension
218
195
219
196
``` python
220
- import plotly.graph_objs as go
197
+ import plotly.graph_objects as go
221
198
import numpyas np
222
199
223
- trace1 = go.Scatter(
200
+ fig = go.Figure( data = go.Scatter(
224
201
y = np.random.randn(500 ),
225
202
mode = ' markers' ,
226
203
marker = dict (
@@ -229,8 +206,8 @@ trace1 = go.Scatter(
229
206
colorscale = ' Viridis' ,# one of plotly colorscales
230
207
showscale = True
231
208
)
232
- )
233
- fig = go.Figure( data = [trace1])
209
+ ))
210
+
234
211
fig.show()
235
212
```
236
213
@@ -240,11 +217,11 @@ Now in Ploty you can implement WebGL with `Scattergl()` in place of `Scatter()`
240
217
for increased speed, improved interactivity, and the ability to plot even more data!
241
218
242
219
``` python
243
- import plotly.graph_objs as go
220
+ import plotly.graph_objects as go
244
221
import numpyas np
245
222
246
223
N= 100000
247
- trace = go.Scattergl(
224
+ fig = go.Figure( data = go.Scattergl(
248
225
x = np.random.randn(N),
249
226
y = np.random.randn(N),
250
227
mode = ' markers' ,
@@ -253,8 +230,30 @@ trace = go.Scattergl(
253
230
colorscale = ' Viridis' ,
254
231
line_width = 1
255
232
)
256
- )
257
- fig= go.Figure(data = [trace])
233
+ ))
234
+
235
+ fig.show()
236
+ ```
237
+
238
+ ``` python
239
+ import plotly.graph_objectsas go
240
+ import numpyas np
241
+
242
+ N= 100000
243
+ r= np.random.uniform(0 ,1 , N)
244
+ theta= np.random.uniform(0 ,2 * np.pi, N)
245
+
246
+ fig= go.Figure(data = go.Scattergl(
247
+ x = r* np.cos(theta),# non-uniform distribution
248
+ y = r* np.sin(theta),# zoom to see more points at the center
249
+ mode = ' markers' ,
250
+ marker = dict (
251
+ color = np.random.randn(N),
252
+ colorscale = ' Viridis' ,
253
+ line_width = 1
254
+ )
255
+ ))
256
+
258
257
fig.show()
259
258
```
260
259