网易首页 > 网易号 > 正文 申请入驻

再见Excel!这个Python数据可视化库太炫酷了

0
分享至

来源:B座17楼

由下面代码生成


from chord import Chord

matrix = [
[0, 5, 6, 4, 7, 4],
[5, 0, 5, 4, 6, 5],
[6, 5, 0, 4, 5, 5],
[4, 4, 4, 0, 5, 5],
[7, 6, 5, 5, 0, 4],
[4, 5, 5, 5, 4, 0],
]

names = ["Action", "Adventure", "Comedy", "Drama", "Fantasy", "Thriller"]

# 保存
Chord(matrix, names).to_html("chord-diagram.html")

图形表现力强悍!

Altair概述

Altair是一个用于Python的声明式统计可视化库,基于Vega和Vega-Lite。

Altair提供了一个强大而简洁的可视化语法,使你能够快速建立一个广泛的统计可视化。下面是一个使用Altair API的例子,通过一个交互式散点图快速实现数据集的可视化。

Github:

https://altair-viz.github.io/getting_started/overview.html

表现强悍

图形表现力强悍!


import matplotlib.pyplot as plt

# 创建数据
size_of_groups = [12, 11, 3, 30]

# 生成饼图
plt.pie(size_of_groups)

# 在中心添加一个圆, 生成环形图
my_circle = plt.Circle((0, 0), 0.7, color='white')
p = plt.gcf()
p.gca().add_artist(my_circle)

plt.show()

image.png


import matplotlib.pyplot as plt
from matplotlib_venn import venn2

# 创建图表
venn2(subsets=(10, 5, 2), set_labels=('Group A', 'Group B'))

# 显示
plt.show()

image.png


import circlify
import matplotlib.pyplot as plt

# 创建画布, 包含一个子图
fig, ax = plt.subplots(figsize=(14, 14))

# 标题
ax.set_title('Repartition of the world population')

# 移除坐标轴
ax.axis('off')

# 人口数据
data = [{'id': 'World', 'datum': 6964195249, 'children': [
{'id': "North America", 'datum': 450448697,
'children': [
{'id': "United States", 'datum': 308865000},
{'id': "Mexico", 'datum': 107550697},
{'id': "Canada", 'datum': 34033000}
]},
{'id': "South America", 'datum': 278095425,
'children': [
{'id': "Brazil", 'datum': 192612000},
{'id': "Colombia", 'datum': 45349000},
{'id': "Argentina", 'datum': 40134425}
]},
{'id': "Europe", 'datum': 209246682,
'children': [
{'id': "Germany", 'datum': 81757600},
{'id': "France", 'datum': 65447374},
{'id': "United Kingdom", 'datum': 62041708}
]},
{'id': "Africa", 'datum': 311929000,
'children': [
{'id': "Nigeria", 'datum': 154729000},
{'id': "Ethiopia", 'datum': 79221000},
{'id': "Egypt", 'datum': 77979000}
]},
{'id': "Asia", 'datum': 2745929500,
'children': [
{'id': "China", 'datum': 1336335000},
{'id': "India", 'datum': 1178225000},
{'id': "Indonesia", 'datum': 231369500}
]}
]}]

# 使用circlify()计算, 获取圆的大小, 位置
circles = circlify.circlify(
data,
show_enclosure=False,
target_enclosure=circlify.Circle(x=0, y=0, r=1)
)

lim = max(
max(
abs(circle.x) + circle.r,
abs(circle.y) + circle.r,
)
for circle in circles
)
plt.xlim(-lim, lim)
plt.ylim(-lim, lim)

for circle in circles:
if circle.level != 2:
continue
x, y, r = circle
ax.add_patch(plt.Circle((x, y), r, alpha=0.5, linewidth=2, color="lightblue"))

for circle in circles:
if circle.level != 3:
continue
x, y, r = circle
label = circle.ex["id"]
ax.add_patch(plt.Circle((x, y), r, alpha=0.5, linewidth=2, color="#69b3a2"))
plt.annotate(label, (x, y), ha='center', color="white")

for circle in circles:
if circle.level != 2:
continue
x, y, r = circle
label = circle.ex["id"]
plt.annotate(label, (x, y), va='center', ha='center', bbox=dict(facecolor='white', edgecolor='black', boxstyle='round', pad=.5))

plt.show()

image.png


import folium
import pandas as pd

# 创建地图对象
m = folium.Map(location=[20,0], tiles="OpenStreetMap", zoom_start=2)

# 坐标点数据
data = pd.DataFrame({
'lon': [-58, 2, 145, 30.32, -4.03, -73.57, 36.82, -38.5],
'lat': [-34, 49, -38, 59.93, 5.33, 45.52, -1.29, -12.97],
'name': ['Buenos Aires', 'Paris', 'melbourne', 'St Petersbourg', 'Abidjan', 'Montreal', 'Nairobi', 'Salvador'],
'value': [10, 12, 40, 70, 23, 43, 100, 43]
}, dtype=str)

# 添加气泡
for i in range(0, len(data)):
folium.Circle(
location=[data.iloc[i]['lat'], data.iloc[i]['lon']],
popup=data.iloc[i]['name'],
radius=float(data.iloc[i]['value'])*20000,
color='crimson',
fill=True,
fill_color='crimson'
).add_to(m)

# 保存
m.save('bubble-map.html')

image.png


import altair as alt
from vega_datasets import data

boroughs = alt.topo_feature(data.londonBoroughs.url, 'boroughs')
tubelines = alt.topo_feature(data.londonTubeLines.url, 'line')
centroids = data.londonCentroids.url

background = alt.Chart(boroughs).mark_geoshape(
stroke='white',
strokeWidth=2
).encode(
color=alt.value('#eee'),
).properties(
width=700,
height=500
)

labels = alt.Chart(centroids).mark_text().encode(
longitude='cx:Q',
latitude='cy:Q',
text='bLabel:N',
size=alt.value(8),
opacity=alt.value(0.6)
).transform_calculate(
"bLabel", "indexof (datum.name,' ') > 0 ? substring(datum.name,0,indexof(datum.name, ' ')) : datum.name"
)

line_scale = alt.Scale(domain=["Bakerloo", "Central", "Circle", "District", "DLR",
"Hammersmith & City", "Jubilee", "Metropolitan", "Northern",
"Piccadilly", "Victoria", "Waterloo & City"],
range=["rgb(137,78,36)", "rgb(220,36,30)", "rgb(255,206,0)",
"rgb(1,114,41)", "rgb(0,175,173)", "rgb(215,153,175)",
"rgb(106,114,120)", "rgb(114,17,84)", "rgb(0,0,0)",
"rgb(0,24,168)", "rgb(0,160,226)", "rgb(106,187,170)"])

lines = alt.Chart(tubelines).mark_geoshape(
filled=False,
strokeWidth=2
).encode(
alt.Color(
'id:N',
legend=alt.Legend(
title=None,
orient='bottom-right',
offset=0
),
scale=line_scale
)
)

background + labels + lines

image.png


import altair as alt
from vega_datasets import data

source = data.disasters.url

alt.Chart(source).mark_circle(
opacity=0.8,
stroke='black',
strokeWidth=1
).encode(
alt.X('Year:O', axis=alt.Axis(labelAngle=0)),
alt.Y('Entity:N'),
alt.Size('Deaths:Q',
scale=alt.Scale(range=[0, 4000]),
legend=alt.Legend(title='Annual Global Deaths')
),
alt.Color('Entity:N', legend=None)
).properties(
width=450,
height=320
).transform_filter(
alt.datum.Entity != 'All natural disasters'
)

image.png


import altair as alt
import pandas as pd

source = pd.DataFrame([
{'country': 'Great Britain', 'animal': 'cattle'},
{'country': 'Great Britain', 'animal': 'cattle'},
{'country': 'Great Britain', 'animal': 'cattle'},
{'country': 'Great Britain', 'animal': 'pigs'},
{'country': 'Great Britain', 'animal': 'pigs'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'pigs'},
{'country': 'United States', 'animal': 'pigs'},
{'country': 'United States', 'animal': 'pigs'},
{'country': 'United States', 'animal': 'pigs'},
{'country': 'United States', 'animal': 'pigs'},
{'country': 'United States', 'animal': 'pigs'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'}
])

domains = ['person', 'cattle', 'pigs', 'sheep']

shape_scale = alt.Scale(
domain=domains,
range=[
'M1.7 -1.7h-0.8c0.3 -0.2 0.6 -0.5 0.6 -0.9c0 -0.6 -0.4 -1 -1 -1c-0.6 0 -1 0.4 -1 1c0 0.4 0.2 0.7 0.6 0.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']
)

alt.Chart(source).mark_point(filled=True, opacity=1, size=100).encode(
alt.X('x:O', axis=None),
alt.Y('animal:O', axis=None),
alt.Row('country:N', header=alt.Header(title='')),
alt.Shape('animal:N', legend=None, scale=shape_scale),
alt.Color('animal:N', legend=None, scale=color_scale),
).transform_window(
x='rank()',
groupby=['country', 'animal']
).properties(width=550, height=140)

提供丰富的图形代码

推荐阅读 点击标题可跳转









特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。

Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.

相关推荐
热点推荐
活到80岁才懂,拼命疼孙子外孙的老人,晚年大多难逃6种结局

活到80岁才懂,拼命疼孙子外孙的老人,晚年大多难逃6种结局

牛锅巴小钒
2026-07-13 05:56:12
丰田“求生欲”拉满!新车油耗4.36L

丰田“求生欲”拉满!新车油耗4.36L

手机评测室
2026-07-14 12:20:39
特朗普发狠沙特参战,美伊多条战线同时交火,绞杀战全面打响

特朗普发狠沙特参战,美伊多条战线同时交火,绞杀战全面打响

阿芒娱乐说
2026-07-14 12:17:53
小玥儿罕露正脸!汪小菲一家游香港,为女儿唱睡前歌!玥儿好开心

小玥儿罕露正脸!汪小菲一家游香港,为女儿唱睡前歌!玥儿好开心

乐悠悠娱乐
2026-07-14 13:05:55
原来这就是升米恩斗米仇!网友:替别人养孩子,怎么都养不熟!

原来这就是升米恩斗米仇!网友:替别人养孩子,怎么都养不熟!

另子维爱读史
2026-06-29 21:27:43
日本海保巡逻舰进入台海引中方不满,日本:不接受中方抗议

日本海保巡逻舰进入台海引中方不满,日本:不接受中方抗议

爱迷彩的老虎
2026-07-14 11:39:56
伟大胜利!温网捷报: 中国金花首夺大满贯冠军 平分奖金690万奖金

伟大胜利!温网捷报: 中国金花首夺大满贯冠军 平分奖金690万奖金

江启
2026-07-13 15:14:27
《百年孤独》最扎心的一段话:陪你熬完这一辈的不是爱情也不是亲情,是你自己都没觉察的内心中的这三种东西

《百年孤独》最扎心的一段话:陪你熬完这一辈的不是爱情也不是亲情,是你自己都没觉察的内心中的这三种东西

心理观察局
2026-07-13 06:56:12
定居香港的舅舅回大陆,炫耀自己住80平豪宅,我:舅舅去我家看看?

定居香港的舅舅回大陆,炫耀自己住80平豪宅,我:舅舅去我家看看?

萧竹轻语
2025-06-26 18:09:53
惊人发现!100%女人被玩之后,不再纠缠,而是无声的进行8种蜕变

惊人发现!100%女人被玩之后,不再纠缠,而是无声的进行8种蜕变

朗威谈星座
2026-07-03 11:08:00
两次拒绝黎明无视钟汉良,20年后55岁憔悴成大妈

两次拒绝黎明无视钟汉良,20年后55岁憔悴成大妈

情感的我
2026-06-10 01:55:14
他是导致台湾难以收复的关键人物,若不是他,台湾或许早就解放了

他是导致台湾难以收复的关键人物,若不是他,台湾或许早就解放了

兵卒史
2026-06-10 04:40:13
伊朗为何不攻以色列却猛打中东美军基地

伊朗为何不攻以色列却猛打中东美军基地

风铃草语
2026-07-14 06:40:55
中共中央决定给予马兴瑞开除党籍、开除公职处分

中共中央决定给予马兴瑞开除党籍、开除公职处分

界面新闻
2026-07-14 11:02:58
听劝!高温天在家,最坑的习惯就是开空调拉着窗帘还开条缝

听劝!高温天在家,最坑的习惯就是开空调拉着窗帘还开条缝

小柱解说游戏
2026-07-11 07:34:50
百万雄师渡江前夜,全世界逼中国停手,唯毛泽东硬扛到底绝不妥协

百万雄师渡江前夜,全世界逼中国停手,唯毛泽东硬扛到底绝不妥协

小莜读史
2026-07-13 13:29:56
俄炼油厂接连被炸,佩斯科夫让记者去问美国:能否让基辅停手

俄炼油厂接连被炸,佩斯科夫让记者去问美国:能否让基辅停手

桂系007
2026-07-13 05:43:08
周星驰问张艺兴:下次我还找你拍戏 , 零片酬行吗 ? 张艺兴回应显情商

周星驰问张艺兴:下次我还找你拍戏 , 零片酬行吗 ? 张艺兴回应显情商

观察鉴娱
2026-07-13 09:40:18
于谦同志个人简历

于谦同志个人简历

乡野小珥
2026-07-14 06:19:37
做过种植牙的牙齿,能管用多少年?看完这篇,再也不瞎保养了

做过种植牙的牙齿,能管用多少年?看完这篇,再也不瞎保养了

荷兰豆爱健康
2026-07-13 10:53:52
2026-07-14 15:23:00
Python小二
Python小二
人生苦短,我用Python!
473文章数 1904关注度
往期回顾 全部

科技要闻

Meta投1.7万亿元建一个AI园区,配10座电厂

头条要闻

施南生去世病因披露 金庸曾称"她将自己奉献给徐克"

头条要闻

施南生去世病因披露 金庸曾称"她将自己奉献给徐克"

体育要闻

33岁成为法国主力,他将在世界杯防守亚马尔

娱乐要闻

75岁施南生走完一生,撑起香港电影黄金时代

财经要闻

赛力斯的“代工厂”困境还能撑多久?

汽车要闻

小米澎程N90 Max工信部信息曝光 全尺寸旗舰 露营版首秀

态度原创

手机
亲子
本地
健康
公开课

手机要闻

三星Galaxy手机在韩独占逾80%份额,亦成为78%用户未来购机选择

亲子要闻

闺女吐槽妈妈生了二胎,自己不带全靠姐姐带,句句经典

本地新闻

打的直达拉萨,一条视频拿下五十万奖金

这几类人离中风最近!有你吗?

公开课

李玫瑾:为什么性格比能力更重要?

无障碍浏览 进入关怀版