我最常用的六個(gè)數(shù)據(jù)可視化工具,強(qiáng)烈推薦
1.Matplotlib
Matplotlib 是一個(gè)用于在 Python 中創(chuàng)建數(shù)據(jù)可視化的庫(kù),常用于繪制圖表、圖形。
它提供了一個(gè)類似 MATLAB 的接口,方便用戶快速繪制各種靜態(tài)、動(dòng)態(tài)和交互式的圖表。
import matplotlib.pyplot as plt
import numpy as np
# 生成數(shù)據(jù)
x = np.arange(10) # X軸數(shù)據(jù)
y = np.random.random(10) # Y軸數(shù)據(jù),生成10個(gè)隨機(jī)數(shù)
# 創(chuàng)建圖形
plt.figure(figsize=(10, 5))
# 繪制折線圖
plt.plot(x, y, label='Random data')
# 添加標(biāo)題和標(biāo)簽
plt.title('Simple Line Plot')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
# 顯示圖例
plt.legend()
# 顯示圖形
plt.show()
2.Seaborn
Seaborn 是一個(gè)基于 Matplotlib 的數(shù)據(jù)可視化庫(kù),它提供了更高級(jí)的接口,使得繪制更加吸引人且富有信息的統(tǒng)計(jì)圖形變得更簡(jiǎn)單。
import seaborn as sns
import matplotlib.pyplot as plt
# 創(chuàng)建示例數(shù)據(jù)
data = {'Category': ['A', 'B', 'C', 'D'],
'Values': [10, 15, 7, 10]}
# 轉(zhuǎn)換為 DataFrame
import pandas as pd
df = pd.DataFrame(data)
# 創(chuàng)建柱狀圖
sns.barplot(x='Category', y='Values', data=df,palette=sns.color_palette("hls", 4))
# 添加標(biāo)題
plt.title('Bar Plot of Categories')
# 顯示圖形
plt.show()
3. Plotly Express
Plotly Express 是 Plotly 的一個(gè)高級(jí)封裝庫(kù),專門用于創(chuàng)建交互式圖表。
它提供了一個(gè)簡(jiǎn)單的語法,能夠快速地生成復(fù)雜的圖表,包括線圖、散點(diǎn)圖、柱狀圖、餅圖等。
Plotly Express 的優(yōu)點(diǎn)在于其強(qiáng)大的交互功能,如懸停提示、縮放和滑動(dòng)條等,這使得它非常適合探索性數(shù)據(jù)分析和呈現(xiàn)動(dòng)態(tài)數(shù)據(jù)。
import plotly.express as px
# 創(chuàng)建示例數(shù)據(jù)
data = {'Category': ['A', 'B', 'C', 'D'],
'Values': [10, 15, 7, 10]}
# 轉(zhuǎn)換為 DataFrame
import pandas as pd
df = pd.DataFrame(data)
# 創(chuàng)建餅圖
fig = px.pie(df, names='Category', values='Values', title='Distribution of Categories')
# 顯示圖形
fig.show()
4.Pygal
Pygal 是一個(gè)用于創(chuàng)建 SVG (Scalable Vector Graphics) 圖表的 Python 庫(kù)。
它設(shè)計(jì)輕巧,易于使用,特別適合 web 應(yīng)用的動(dòng)態(tài)圖表。
Pygal 提供了各種圖表類型,如條形圖、線形圖、餅圖、雷達(dá)圖和散點(diǎn)圖等。
它的主要優(yōu)點(diǎn)之一是可以直接在瀏覽器中查看動(dòng)態(tài)可縮放的圖形,這對(duì)于需要在 web 頁面上直接嵌入圖形的應(yīng)用來說非常有用。
import pygal
# 創(chuàng)建散點(diǎn)圖對(duì)象
scatter_chart = pygal.XY(stroke=False)
# 圖表標(biāo)題
scatter_chart.title = 'Sample Scatter Plot'
# 添加數(shù)據(jù)點(diǎn)
scatter_chart.add('A', [(1, 2), (4, 7), (5, 3)])
scatter_chart.add('B', [(2, 3), (3, 12), (6, 8)])
# 保存圖表到文件,也可以使用 render_to_file('scatter_chart.svg') 方法
scatter_chart.render_to_file('scatter_chart.svg')
5.Altair
Altair 是一個(gè)聲明式的統(tǒng)計(jì)可視化庫(kù)。
它建立在強(qiáng)大的 Vega-Lite 可視化語法之上,使得創(chuàng)建復(fù)雜和美觀的圖表變得簡(jiǎn)單而直觀。
Altair 的主要優(yōu)點(diǎn)是其聲明式語法:你只需描述圖表的各個(gè)組成部分,如數(shù)據(jù)和編碼,而無需擔(dān)心底層實(shí)現(xiàn)細(xì)節(jié)。
這使得 Altair 非常適合于數(shù)據(jù)探索和快速可視化。
import altair as alt
from vega_datasets import data
# 加載鳶尾花數(shù)據(jù)集
iris = data.iris()
# 使用melt將數(shù)據(jù)從寬格式轉(zhuǎn)換為長(zhǎng)格式
iris_long = iris.melt(id_vars='species', var_name='Measurement_type', value_name='value')
# 創(chuàng)建密度圖
density_plot = alt.Chart(iris_long).transform_density(
'value',
as_=['value', 'density'],
groupby=['Measurement_type']
).mark_area(opacity=0.5).encode(
alt.X('value:Q'),
alt.Y('density:Q'),
alt.Color('Measurement_type:N')
).properties(
title='Density Plot'
)
density_plot
6.Bokeh
Bokeh 是一個(gè)用于創(chuàng)建交互式和可視化圖表的 Python 庫(kù),特別適合在瀏覽器中展示。
它能夠處理大型數(shù)據(jù)集或?qū)崟r(shí)數(shù)據(jù)集,支持快速繪制,并且可以輕松地嵌入到 HTML 頁面中。
from bokeh.plotting import figure, show, output_file
from bokeh.models import ColumnDataSource
import pandas as pd
# 準(zhǔn)備數(shù)據(jù)
data = pd.DataFrame({
'x': [1, 2, 3, 4, 5],
'y1': [2, 5, 8, 2, 7],
'y2': [3, 5, 7, 8, 6]
})
# 創(chuàng)建一個(gè)繪圖
p = figure(width=600, height=400)
# 添加面積圖,使用鮮艷的顏色
p.varea_stack(['y1', 'y2'], x='x', color=("blue", "orange"), source=data)
# 顯示結(jié)果
output_file("area_chart.html")
show(p)