【Python】高大上!5个python高级可视化图表!
共 7465字,需浏览 15分钟
·
2024-05-25 12:00
以下示例代码使用Holoviews & Bokeh创建和弦图,展示5个不同国家之间的贸易关系。
import holoviews as hvfrom holoviews import optsimport pandas as pdimport numpy as nphv.extension('bokeh')# Sample matrix representing the export volumes between 5 countriesexport_data = np.array([[0, 50, 30, 20, 10],[10, 0, 40, 30, 20],[20, 10, 0, 35, 25],[30, 20, 10, 0, 40],[25, 15, 30, 20, 0]])labels = ['USA', 'China', 'Germany', 'Japan', 'India']# Creating a pandas DataFramedf = pd.DataFrame(export_data, index=labels, columns=labels)df = df.stack().reset_index()df.columns = ['source', 'target', 'value']# Creating a Chord objectchord = hv.Chord(df)# Styling the Chord diagramchord.opts(opts.Chord(cmap='Category20', edge_cmap='Category20',labels='source', label_text_font_size='10pt',edge_color='source', node_color='index',width=700, height=700)).select(value=(5, None))# Display the plotchord
https://holoviews.org/reference/elements/matplotlib/Chord.html
https://github.com/moshi4/pyCirclize
2. 旭日图(Sunburst Chart)
import plotly.express as pximport numpy as npdf = px.data.gapminder().query("year == 2007")fig = px.sunburst(df, path=['continent', 'country'],values='pop',color='lifeExp',hover_data=['iso_alpha'],color_continuous_scale='RdBu',color_continuous_midpoint=np.average(df['lifeExp'], weights=df['pop']))fig.show()
https://plotly.com/python/sunburst-charts/
3. 六边形箱图(Hexbin Plot)
六边形箱图(通常称为六边形分箱图或六边形热力图)是一种非常有效的可视化工具,用于展示二维数据分布,尤其是在数据点过于密集而导致传统散点图难以区分单个点的情况下。这种方法将数据空间划分为一系列六边形单元(或“箱”),并根据每个单元中的数据点数量进行着色,从而提供了数据分布情况的直观表示。
import numpy as npimport matplotlib.pyplot as pltfrom mplhexbin import HexBin# 假设的模拟数据np.random.seed(0) # 确保结果可复现n_points = 10000x = np.random.rand(n_points) * 100 # 空气质量指数(AQI)范围从0到100y = 5 * np.sin(x * np.pi / 50) + np.random.randn(n_points) * 15 # 假设的医院就诊数量,与AQI相关但具有噪声# 创建一个新的图形fig, ax = plt.subplots(figsize=(10, 8))# 使用HexBin创建六边形分箱图hb = HexBin(ax, gridsize=20, cmap='viridis', extent=[0, 100, -30, 50]) # 设置网格大小、颜色映射和范围hb.hexbin(x, y, mincnt=1) # 绘制六边形分箱图,mincnt设置最小计数阈值# 添加标题和轴标签ax.set_title('空气质量指数(AQI)与医院就诊数量的关系')ax.set_xlabel('空气质量指数(AQI)')ax.set_ylabel('医院就诊数量')# 显示图形plt.colorbar(hb.cmap, ax=ax, label='数据点数量') # 添加颜色条并设置标签plt.show()
https://matplotlib.org/stable/gallery/statistics/hexbin_demo.html
4. 桑基图(Sankey Diagram)
Plotly库可以用来创建如下所示的桑基图。以下代码表示能量从生产源流向小城市的消费者。
import plotly.graph_objects as golabels = ["Coal", "Solar", "Wind", "Nuclear", "Residential", "Industrial", "Commercial"]source = [0, 1, 2, 3, 0, 1, 2, 3]target = [4, 4, 4, 4, 5, 5, 5, 5]value = [25, 10, 40, 20, 30, 15, 25, 35]# Create the Sankey diagram objectfig = go.Figure(data=[go.Sankey(node=dict(pad=15,thickness=20,line=dict(color="black", width=0.5),label=labels),link=dict(source=source,target=target,value=value))])fig.update_layout(title_text="Energy Flow in Model City", font_size=12)fig.show()
https://plotly.com/python/sankey-diagram/
5. 主题河流图(Stream Graph/ Theme River)
Altair数据可视化库可用于绘制流图,如下所示。
import altair as altfrom vega_datasets import datasource = data.unemployment_across_industries.urlalt.Chart(source).mark_area().encode(alt.X('yearmonth(date):T',axis=alt.Axis(format='%Y', domain=False, tickSize=0)),alt.Y('sum(count):Q', stack='center', axis=None),alt.Color('series:N',scale=alt.Scale(scheme='category20b'))).interactive()
https://altair-viz.github.io/gallery/streamgraph.html
往期精彩回顾 
交流群
欢迎加入机器学习爱好者微信群一起和同行交流,目前有机器学习交流群、博士群、博士申报交流、CV、NLP等微信群,请扫描下面的微信号加群,备注:”昵称-学校/公司-研究方向“,例如:”张小明-浙大-CV“。请按照格式备注,否则不予通过。添加成功后会根据研究方向邀请进入相关微信群。请勿在群内发送广告,否则会请出群,谢谢理解~(也可以加入机器学习交流qq群772479961)
