Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdfbd88f

Browse files
committed
graph_objects, update_layout and better examples
1 parent1e8abfc commitdfbd88f

File tree

2 files changed

+202
-171
lines changed

2 files changed

+202
-171
lines changed

‎notebooks/line-and-scatter.md

Lines changed: 85 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -35,61 +35,58 @@ jupyter:
3535
redirect_from:python/line-and-scatter-plots-tutorial/
3636
thumbnail:thumbnail/line-and-scatter.jpg
3737
title:Python Scatter Plots | plotly
38+
v4upgrade:true
3839
---
3940

4041
##Scatter plot with plotly express
4142

4243
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.
4344

4445
```python
45-
importplotly_expressas px
46+
importplotly.expressas px
4647
iris= px.data.iris()
4748
fig= px.scatter(iris,x="sepal_width",y="sepal_length")
4849
fig.show()
4950
```
5051

5152
####Set size and color with column names
5253

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`.
5455

5556
```python
56-
importplotly_expressas px
57+
importplotly.expressas px
5758
iris= px.data.iris()
5859
fig= px.scatter(iris,x="sepal_width",y="sepal_length",color="species",
59-
size='petal_length')
60+
size='petal_length',hover_data=['petal_width'])
6061
fig.show()
6162
```
6263

6364
##Line plot with plotly express
6465

6566
```python
66-
importplotly_expressas px
67+
importplotly.expressas px
6768
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()
6971
```
7072

7173
##Scatter and line plot with go.Scatter
7274

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).
7476

7577

7678
####Simple Scatter Plot
7779

7880
```python
79-
import plotly.graph_objsas go
80-
81-
# Create random data with numpy
81+
import plotly.graph_objectsas go
8282
import numpyas np
8383

8484
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)
8787

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'))
9189

92-
fig= go.Figure(data=[trace])
9390
fig.show()
9491
```
9592

@@ -98,7 +95,7 @@ fig.show()
9895
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/).
9996

10097
```python
101-
import plotly.graph_objsas go
98+
import plotly.graph_objectsas go
10299

103100
# Create random data with numpy
104101
import numpyas np
@@ -109,18 +106,19 @@ random_y0 = np.random.randn(N) + 5
109106
random_y1= np.random.randn(N)
110107
random_y2= np.random.randn(N)-5
111108

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,
114113
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,
117116
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,
120119
mode='lines',
121-
name='lines')
120+
name='lines'))
122121

123-
fig= go.Figure(data=[trace0, trace1, trace2])
124122
fig.show()
125123
```
126124

@@ -129,98 +127,77 @@ fig.show()
129127
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/)
130128

131129
```python
132-
import plotly.graph_objsas go
130+
import plotly.graph_objectsas go
133131

134-
trace0= go.Scatter(
132+
fig=go.Figure(data=go.Scatter(
135133
x=[1,2,3,4],
136134
y=[10,11,12,13],
137135
mode='markers',
138136
marker=dict(size=[40,60,80,100],
139137
color=[0,1,2,3])
140-
)
138+
))
141139

142-
fig= go.Figure(data=[trace0])
143140
fig.show()
144141
```
145142

146143
####Style Scatter Plots
147144

148145
```python
149-
import plotly.graph_objsas go
146+
import plotly.graph_objectsas go
150147
import numpyas np
151148

152-
N=500
153149

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()
164153

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',
169157
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)
172171

173-
layout= go.Layout(title='Styled Scatter',
174-
yaxis_zeroline=False,xaxis_zeroline=False)
175172

176-
fig= go.Figure(data=[trace0, trace1],layout=layout)
177173
fig.show()
178174
```
179175

180176
####Data Labels on Hover
181177

182178
```python
183-
import plotly.graph_objsas go
184-
import random
185-
import numpyas np
179+
import plotly.graph_objectsas go
186180
import pandasas pd
187181

188-
l= []
189-
y= []
190182
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 iinrange(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')
214191
fig.show()
215192
```
216193

217194
####Scatter with a Color Dimension
218195

219196
```python
220-
import plotly.graph_objsas go
197+
import plotly.graph_objectsas go
221198
import numpyas np
222199

223-
trace1= go.Scatter(
200+
fig=go.Figure(data=go.Scatter(
224201
y= np.random.randn(500),
225202
mode='markers',
226203
marker=dict(
@@ -229,8 +206,8 @@ trace1 = go.Scatter(
229206
colorscale='Viridis',# one of plotly colorscales
230207
showscale=True
231208
)
232-
)
233-
fig= go.Figure(data=[trace1])
209+
))
210+
234211
fig.show()
235212
```
236213

@@ -240,11 +217,11 @@ Now in Ploty you can implement WebGL with `Scattergl()` in place of `Scatter()`
240217
for increased speed, improved interactivity, and the ability to plot even more data!
241218

242219
```python
243-
import plotly.graph_objsas go
220+
import plotly.graph_objectsas go
244221
import numpyas np
245222

246223
N=100000
247-
trace= go.Scattergl(
224+
fig=go.Figure(data=go.Scattergl(
248225
x= np.random.randn(N),
249226
y= np.random.randn(N),
250227
mode='markers',
@@ -253,8 +230,30 @@ trace = go.Scattergl(
253230
colorscale='Viridis',
254231
line_width=1
255232
)
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+
258257
fig.show()
259258
```
260259

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp