数据可视化技术1. 技术分析1.1 数据可视化概述数据可视化是数据科学的重要组成部分可视化类型 探索性可视化: 发现模式 解释性可视化: 传达信息 交互式可视化: 探索数据 可视化方法: 统计图表: 直方图、箱线图 关系图表: 散点图、折线图 地理图表: 地图 层次图表: 树状图、桑基图1.2 可视化原则设计原则 清晰性: 简洁明了 准确性: 数据正确 美观性: 视觉吸引力 一致性: 风格统一 颜色使用: 分类数据: 定性调色板 连续数据: 渐变调色板 顺序数据: 顺序调色板1.3 可视化工具对比工具类型特点适用场景matplotlib基础灵活快速绘图seaborn统计美观统计图表plotly交互动态探索分析TableauBI专业商业报告2. 核心功能实现2.1 统计图表import matplotlib.pyplot as plt import seaborn as sns import pandas as pd class StatisticalVisualizer: def __init__(self, df): self.df df def plot_histogram(self, column, bins30, titleNone): plt.figure(figsize(10, 6)) sns.histplot(dataself.df, xcolumn, binsbins, kdeTrue) if title: plt.title(title) else: plt.title(fDistribution of {column}) plt.xlabel(column) plt.ylabel(Frequency) plt.show() def plot_boxplot(self, column, byNone, titleNone): plt.figure(figsize(10, 6)) if by: sns.boxplot(dataself.df, xby, ycolumn) if title: plt.title(title) else: plt.title(f{column} by {by}) else: sns.boxplot(dataself.df, ycolumn) if title: plt.title(title) else: plt.title(fBoxplot of {column}) plt.show() def plot_barplot(self, x, y, hueNone, titleNone): plt.figure(figsize(10, 6)) sns.barplot(dataself.df, xx, yy, huehue) if title: plt.title(title) else: plt.title(f{y} by {x}) plt.xticks(rotation45) plt.tight_layout() plt.show() def plot_violinplot(self, x, y, titleNone): plt.figure(figsize(10, 6)) sns.violinplot(dataself.df, xx, yy) if title: plt.title(title) else: plt.title(fViolin Plot: {y} by {x}) plt.show()2.2 关系图表class RelationshipVisualizer: def __init__(self, df): self.df df def plot_scatter(self, x, y, hueNone, sizeNone, titleNone): plt.figure(figsize(10, 6)) sns.scatterplot(dataself.df, xx, yy, huehue, sizesize) if title: plt.title(title) else: plt.title(f{x} vs {y}) plt.show() def plot_line(self, x, y, hueNone, styleNone, titleNone): plt.figure(figsize(12, 6)) sns.lineplot(dataself.df, xx, yy, huehue, stylestyle, markero) if title: plt.title(title) else: plt.title(fTrend of {y} over {x}) plt.show() def plot_heatmap(self, correlation_matrixNone, titleNone): if correlation_matrix is None: numeric_df self.df.select_dtypes(include[number]) correlation_matrix numeric_df.corr() plt.figure(figsize(12, 10)) sns.heatmap(correlation_matrix, annotTrue, cmapcoolwarm, vmin-1, vmax1) if title: plt.title(title) else: plt.title(Correlation Heatmap) plt.show() def plot_pairplot(self, columnsNone, hueNone, titleNone): if columns is None: columns self.df.select_dtypes(include[number]).columns g sns.pairplot(self.df[columns], huehue) if title: g.fig.suptitle(title, y1.02) plt.show()2.3 交互式可视化import plotly.express as px class InteractiveVisualizer: def __init__(self, df): self.df df def plot_interactive_histogram(self, column, titleNone): fig px.histogram(self.df, xcolumn, nbins30, titletitle) fig.show() def plot_interactive_scatter(self, x, y, colorNone, sizeNone, titleNone): fig px.scatter(self.df, xx, yy, colorcolor, sizesize, titletitle) fig.show() def plot_interactive_line(self, x, y, colorNone, titleNone): fig px.line(self.df, xx, yy, colorcolor, markersTrue, titletitle) fig.show() def plot_interactive_choropleth(self, locations, locationmode, color, titleNone): fig px.choropleth( self.df, locationslocations, locationmodelocationmode, colorcolor, titletitle ) fig.show() def plot_interactive_treemap(self, path, values, colorNone, titleNone): fig px.treemap(self.df, pathpath, valuesvalues, colorcolor, titletitle) fig.show()2.4 高级可视化class AdvancedVisualizer: def __init__(self, df): self.df df def plot_wordcloud(self, text_column, titleNone): from wordcloud import WordCloud text .join(self.df[text_column].dropna().astype(str)) wordcloud WordCloud(width800, height400, background_colorwhite).generate(text) plt.figure(figsize(12, 6)) plt.imshow(wordcloud, interpolationbilinear) plt.axis(off) if title: plt.title(title) plt.show() def plot_sankey(self, source, target, value, titleNone): import plotly.graph_objects as go fig go.Figure(data[go.Sankey( nodedict( pad15, thickness20, linedict(colorblack, width0.5), labellist(set(self.df[source].unique()) | set(self.df[target].unique())) ), linkdict( sourceself.df[source].astype(category).cat.codes, targetself.df[target].astype(category).cat.codes, valueself.df[value] ) )]) fig.update_layout(title_texttitle if title else Sankey Diagram, font_size10) fig.show() def plot_3d_scatter(self, x, y, z, colorNone, titleNone): fig px.scatter_3d(self.df, xx, yy, zz, colorcolor, titletitle) fig.show() def plot_candlestick(self, date, open, high, low, close, titleNone): import plotly.graph_objects as go fig go.Figure(data[go.Candlestick( xself.df[date], openself.df[open], highself.df[high], lowself.df[low], closeself.df[close] )]) fig.update_layout(title_texttitle if title else Candlestick Chart, xaxis_rangeslider_visibleFalse) fig.show()3. 性能对比3.1 可视化工具对比工具美观度交互性学习曲线matplotlib中低低seaborn高低中plotly很高高中ggplot高低中3.2 图表类型选择数据类型推荐图表用途单变量分布直方图展示分布双变量关系散点图展示关系分类比较箱线图比较分布时间序列折线图展示趋势3.3 颜色方案对比方案适用场景示例定性分类数据不同类别用不同颜色顺序数值数据从低到高渐变发散对比数据正负值对比4. 最佳实践4.1 可视化流程def visualization_pipeline(df): # 1. 单变量分析 visualizer StatisticalVisualizer(df) for col in df.select_dtypes(include[number]).columns[:3]: visualizer.plot_histogram(col) # 2. 关系分析 rel_visualizer RelationshipVisualizer(df) rel_visualizer.plot_heatmap() # 3. 交互式探索 interactive InteractiveVisualizer(df) numeric_cols df.select_dtypes(include[number]).columns if len(numeric_cols) 2: interactive.plot_interactive_scatter(numeric_cols[0], numeric_cols[1])4.2 图表美化def configure_plot_style(): sns.set_style(whitegrid) sns.set_palette(viridis) plt.rcParams[font.family] SimHei plt.rcParams[axes.unicode_minus] False plt.rcParams[figure.dpi] 100 plt.rcParams[savefig.dpi] 3005. 总结数据可视化是数据科学的关键环节统计图表展示数据分布关系图表探索变量关系交互式可视化深入探索数据高级可视化展示复杂关系对比数据如下seaborn绘制最美观的统计图表plotly提供最佳交互体验需要根据数据类型选择合适图表推荐先探索再解释良好的可视化可以帮助理解数据和传达洞察。