一文教你利用Python畫花樣圖

前言

在之前的一篇文章Python可視化神器-Plotly動畫展示展現瞭可視化神器-Plotly的動畫的基本應用,本文介紹如何在Python中使用 Plotly 創建地圖並在地圖上標相應的線。

地球儀加線

根據地球儀的區域顯示在相應的位置圖形上加上線條,完美的線性地球儀詳細代碼如下:

`import plotly.express as px
df = px.data.gapminder.query("year == 2007")
fig = px.line_geo(df, locations="iso_alpha",
color="continent", 
# "continent" is one of the columns of gapminder
projection="orthographic")
fig.show` 

顯示結果為:**

圖片.png

地圖上加線

繪畫出相應的地圖後添加經緯度,再根據經緯度繪畫出相應的線條,詳細代碼如下:

import

plotly.graph_objects

as

go

fig = go.Figure(data=go.Scattergeo(
lat = [3.86, 53.55],
lon = [73.66, 135.05],
mode = 'lines',
line = dict(width = 2, color = 'red'),
))

fig.update_layout(
geo = dict(
resolution = 50,
showland = True,
showlakes = True,
landcolor = 'rgb(203, 203, 203)',
countrycolor = 'rgb(204, 204, 204)',
lakecolor = 'rgb(255, 255, 255)',
projection_type = "equirectangular",
coastlinewidth = 3,
lataxis = dict(
range = [20, 60],
showgrid = True,
dtick = 10
),
lonaxis = dict(
range = [-100, 20],
showgrid = True,
dtick = 20
),
)
)

`fig.show`

顯示結果如下:

圖片.png

圖片.png

最後的福利-3D圖鑒賞

最後加入一個3D圖像鑒賞,制作圖像詳細代碼如下:

# 導入包import

plotly.graph_objects

as

go

from

plotly.subplots

import

make_subplots

import

numpy

as

np

N = 50

fig = make_subplots(rows=2, cols=2,
specs=[[{'is_3d': True}, {'is_3d': True}],
[{'is_3d': True}, {'is_3d': True}]],
print_grid=False)
for i in [1,2]:
for j in [1,2]:
fig.append_trace(
go.Mesh3d(
x=(50*np.random.randn(N)),
y=(20*np.random.randn(N)),
z=(40*np.random.randn(N)),
opacity=0.5,
),
row=i, col=j)

`fig.update_layout(width=700, margin=dict(r=9, l=9, b=9, t=9))
# 將左上角子圖中的比率固定為立方體
fig.update_layout(scene_aspectmode='cube')
# 手動強制z軸顯示為其他兩個的兩倍大
fig.update_layout(scene2_aspectmode='manual',
scene2_aspectratio=dict(x=1, y=1, z=2))
# 繪制軸線與軸線范圍的比例成比例
fig.update_layout(scene3_aspectmode='data')
# 使用“data”作為默認值自動生成比例良好的內容
fig.update_layout(scene4_aspectmode='auto')
#顯示
fig.show` 

顯示結果如下:

圖片.png

圖片.png

總結

到此這篇關於利用Python畫花樣圖的文章就介紹到這瞭,更多相關Python畫花樣圖內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: