plotly系列(5)-- 高级封装

Plotly Express是Plotly的高级封装,它为复杂的图表绘制提供了一种简单的语法。

一、数据分布

1、直方图

1
2
3
4
5
6
7
8
9
10
11
12
13
import numpy as np
import plotly as py
import plotly.express as px
import plotly.graph_objs as go

tips = px.data.tips()

fig = px.histogram(
tips, x="total_bill", y="tip", color="sex",
marginal="rug", hover_data=tips.columns
)

py.offline.plot(fig, filename='test.html', auto_open=True)

2、箱形图

1
2
3
4
5
6
7
8
9
10
import numpy as np
import plotly as py
import plotly.express as px
import plotly.graph_objs as go

tips = px.data.tips()

fig = px.box(tips, x="day", y="total_bill", color="smoker", notched=True)

py.offline.plot(fig, filename='test.html', auto_open=True)

3、小提琴图

1
2
3
4
5
6
7
8
9
10
11
12
13
import numpy as np
import plotly as py
import plotly.express as px
import plotly.graph_objs as go

tips = px.data.tips()

fig = px.violin(
tips, y="tip", x="smoker", color="sex",
box=True, points="all", hover_data=tips.columns
)

py.offline.plot(fig, filename='test.html', auto_open=True)

二、数据比较

1、柱形图

1
2
3
4
5
6
7
8
9
10
import numpy as np
import plotly as py
import plotly.express as px
import plotly.graph_objs as go

tips = px.data.tips()

fig = px.bar(tips, x="sex", y="total_bill", color="smoker", barmode="group")

py.offline.plot(fig, filename='test.html', auto_open=True)

2、分组柱形图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import numpy as np
import plotly as py
import plotly.express as px
import plotly.graph_objs as go

tips = px.data.tips()

fig = px.bar(
tips, x="sex", y="total_bill", color="smoker",
barmode="group", facet_row="time", facet_col="day",
category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "time": ["Lunch", "Dinner"]}
)

py.offline.plot(fig, filename='test.html', auto_open=True)

3、雷达图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import pandas as pd
import plotly as py
import plotly.express as px
import plotly.graph_objs as go

df = pd.DataFrame(dict(
r=[1, 5, 2, 2, 3],
theta=['processing cost','mechanical properties','chemical stability', 'thermal stability', 'device integration'])
)

fig = px.line_polar(df, r='r', theta='theta', line_close=True)
# fig.update_traces(fill='toself')

py.offline.plot(fig, filename='test.html', auto_open=True)

4、折线图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import plotly as py
import plotly.express as px
import plotly.graph_objs as go

gapminder = px.data.gapminder()

# 选取2个国家:中国、阿富汗
country_list = ["China", "Afghanistan"]
tem_gapminder = gapminder.query("country == @country_list")

fig = px.line(
tem_gapminder, x="year", y="lifeExp", color="country",
line_shape="spline", line_dash="country", title="国家人均寿命"
)
# 修改时添加的代码
fig.update_traces(dict(mode="markers+lines"))
fig.update(layout=dict(xaxis=dict(title="年份", showline=True, nticks=10), yaxis=dict(title="人均寿命", showline=True, nticks=10)))

py.offline.plot(fig, filename='test.html', auto_open=True)

三、数据关系

1、带边缘的散点图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import numpy as np
import plotly as py
import plotly.express as px
import plotly.graph_objs as go

iris = px.data.iris()

fig = px.scatter(
iris, x="sepal_width", y="sepal_length", color="species",
marginal_y="violin", marginal_x="box",
trendline="ols"
)

py.offline.plot(fig, filename='test.html', auto_open=True)

2、矩阵散点图

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np
import plotly as py
import plotly.express as px
import plotly.graph_objs as go

iris = px.data.iris()

fig = px.scatter_matrix(
iris, dimensions=["sepal_width", "sepal_length", "petal_width", "petal_length"], color="species"
)

py.offline.plot(fig, filename='test.html', auto_open=True)

3、带时间轴的散点图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import numpy as np
import plotly as py
import plotly.express as px
import plotly.graph_objs as go

gapminder = px.data.gapminder()

fig = px.scatter(
gapminder, x="gdpPercap", y="lifeExp", animation_frame="year",
animation_group="country", size="pop", color="continent",
hover_name="country", facet_col="continent",log_x=True,
size_max=45, range_x=[100,100000], range_y=[25,90]
)

py.offline.plot(fig, filename='test.html', auto_open=True)

4、三元散点图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import numpy as np
import plotly as py
import plotly.express as px
import plotly.graph_objs as go

election = px.data.election()

fig = px.scatter_ternary(
election, a="Joly", b="Coderre", c="Bergeron", color="winner",
size="total", hover_name="district", size_max=15,
color_discrete_map = {"Joly": "blue", "Bergeron": "green", "Coderre":"red"}
)

py.offline.plot(fig, filename='test.html', auto_open=True)

5、平行坐标图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import numpy as np
import plotly as py
import plotly.express as px
import plotly.graph_objs as go

iris = px.data.iris()

fig = px.parallel_coordinates(
iris, color="species_id",
labels={"species_id": "Species", "sepal_width": "Sepal Width", "sepal_length": "Sepal Length", "petal_width": "Petal Width", "petal_length": "Petal Length"},
color_continuous_scale=px.colors.diverging.Tealrose, color_continuous_midpoint=2
)

py.offline.plot(fig, filename='test.html', auto_open=True)

6、并行类别图

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np
import plotly as py
import plotly.express as px
import plotly.graph_objs as go

tips = px.data.tips()

fig = px.parallel_categories(
tips, color="size", color_continuous_scale=px.colors.sequential.Inferno
)

py.offline.plot(fig, filename='test.html', auto_open=True)

四、极坐标

1、极坐标散点图

1
2
3
4
5
6
7
8
9
import plotly as py
import plotly.express as px
import plotly.graph_objs as go

wind = px.data.wind()

fig = px.scatter_polar(wind, r="frequency", theta="direction")

py.offline.plot(fig, filename='test.html', auto_open=True)

2、极坐标条形图

1
2
3
4
5
6
7
8
9
10
11
12
13
import plotly as py
import plotly.express as px
import plotly.graph_objs as go

wind = px.data.wind()

fig = px.bar_polar(
wind, r="frequency", theta="direction",
color="strength", template="plotly_dark",
color_discrete_sequence= px.colors.sequential.Plasma_r
)

py.offline.plot(fig, filename='test.html', auto_open=True)

五、地理

1、地理坐标散点图

1
2
3
4
5
6
7
8
9
10
11
12
import plotly as py
import plotly.express as px
import plotly.graph_objs as go

gapminder = px.data.gapminder()

fig = px.scatter_geo(
gapminder, locations="iso_alpha", color="continent", hover_name="country",
size="pop", animation_frame="year", projection="natural earth"
)

py.offline.plot(fig, filename='test.html', auto_open=True)

2、地理区域图

1
2
3
4
5
6
7
8
9
10
11
12
import plotly as py
import plotly.express as px
import plotly.graph_objs as go

gapminder = px.data.gapminder()

fig = px.choropleth(
gapminder, locations="iso_alpha", color="lifeExp", hover_name="country",
animation_frame="year",color_continuous_scale=px.colors.sequential.Plasma, range_color=[20,80]
)

py.offline.plot(fig, filename='test.html', auto_open=True)

六、箭头图

1、基本的箭头图

1
2
3
4
5
6
7
8
9
10
import plotly.figure_factory as ff
import numpy as np

x,y = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2))
u = np.cos(x)*y
v = np.sin(x)*y

fig = ff.create_quiver(x, y, u, v)

py.offline.plot(fig, filename='test.html', auto_open=True)

2、带中心点的箭头图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import plotly as py
import plotly.figure_factory as ff
import plotly.graph_objects as go
import numpy as np

x, y = np.meshgrid(np.arange(-2, 2, .2), np.arange(-2, 2, .25))
z = x*np.exp(-x**2 - y**2)
v, u = np.gradient(z, .2, .2)

# 创建箭头图
fig = ff.create_quiver(
x, y, u, v,
scale=.25, arrow_scale=.4, name='quiver', line_width=1
)

# 往图中添加点
fig.add_trace(
go.Scatter(x=[-.7, .75], y=[0,0], mode='markers', marker_size=12, name='points')
)

py.offline.plot(fig, filename='test.html', auto_open=True)
0%