25 个常用 Matplotlib 图的 Python 代码(二)

简介: 大家好,今天要分享给大家25个Matplotlib图的汇总,在数据分析和可视化中非常有用,文章较长,可以马起来慢慢练手。

4. 抖动图

通常,多个数据点具有完全相同的X和Y值。结果,多个点相互绘制并隐藏。为避免这种情况,请稍微抖动点,以便您可以直观地看到它们。这很方便使用

# Import Data

df = pd.read_csv("https://raw.githubusercontent.com/selva86/datasets/master/mpg_ggplot2.csv")


# Draw Stripplot

fig, ax = plt.subplots(figsize=(16,10), dpi= 80)    

sns.stripplot(df.cty, df.hwy, jitter=0.25, size=8, ax=ax, linewidth=.5)


# Decorations

plt.title('Use jittered plots to avoid overlapping of points', fontsize=22)

plt.show()

26.jpg

5. 计数图

避免点重叠问题的另一个选择是增加点的大小,这取决于该点中有多少点。因此,点的大小越大,周围的点的集中度就越大。

# Import Data

df = pd.read_csv("https://raw.githubusercontent.com/selva86/datasets/master/mpg_ggplot2.csv")

df_counts = df.groupby(['hwy', 'cty']).size().reset_index(name='counts')


# Draw Stripplot

fig, ax = plt.subplots(figsize=(16,10), dpi= 80)    

sns.stripplot(df_counts.cty, df_counts.hwy, size=df_counts.counts*2, ax=ax)


# Decorations

plt.title('Counts Plot - Size of circle is bigger as more points overlap', fontsize=22)

plt.show()


27.jpg

6. 边缘直方图

边缘直方图具有沿X和Y轴变量的直方图。这用于可视化X和Y之间的关系以及单独的X和Y的单变量分布。该图如果经常用于探索性数据分析(EDA)。

# Import Data

df = pd.read_csv("https://raw.githubusercontent.com/selva86/datasets/master/mpg_ggplot2.csv")


# Create Fig and gridspec

fig = plt.figure(figsize=(16, 10), dpi= 80)

grid = plt.GridSpec(4, 4, hspace=0.5, wspace=0.2)


# Define the axes

ax_main = fig.add_subplot(grid[:-1, :-1])

ax_right = fig.add_subplot(grid[:-1, -1], xticklabels=[], yticklabels=[])

ax_bottom = fig.add_subplot(grid[-1, 0:-1], xticklabels=[], yticklabels=[])


# Scatterplot on main ax

ax_main.scatter('displ', 'hwy', s=df.cty*4, c=df.manufacturer.astype('category').cat.codes, alpha=.9, data=df, cmap="tab10", edgecolors='gray', linewidths=.5)


# histogram on the right

ax_bottom.hist(df.displ, 40, histtype='stepfilled', orientation='vertical', color='deeppink')

ax_bottom.invert_yaxis()


# histogram in the bottom

ax_right.hist(df.hwy, 40, histtype='stepfilled', orientation='horizontal', color='deeppink')


# Decorations

ax_main.set(title='Scatterplot with Histograms
displ vs hwy'
, xlabel='displ', ylabel='hwy')

ax_main.title.set_fontsize(20)

for item in ([ax_main.xaxis.label, ax_main.yaxis.label] + ax_main.get_xticklabels() + ax_main.get_yticklabels()):

   item.set_fontsize(14)


xlabels = ax_main.get_xticks().tolist()

ax_main.set_xticklabels(xlabels)

plt.show()


28.jpg7.边缘箱形图

边缘箱图与边缘直方图具有相似的用途。然而,箱线图有助于精确定位X和Y的中位数,第25和第75百分位数。

# Import Data

df = pd.read_csv("https://raw.githubusercontent.com/selva86/datasets/master/mpg_ggplot2.csv")


# Create Fig and gridspec

fig = plt.figure(figsize=(16, 10), dpi= 80)

grid = plt.GridSpec(4, 4, hspace=0.5, wspace=0.2)


# Define the axes

ax_main = fig.add_subplot(grid[:-1, :-1])

ax_right = fig.add_subplot(grid[:-1, -1], xticklabels=[], yticklabels=[])

ax_bottom = fig.add_subplot(grid[-1, 0:-1], xticklabels=[], yticklabels=[])


# Scatterplot on main ax

ax_main.scatter('displ', 'hwy', s=df.cty*5, c=df.manufacturer.astype('category').cat.codes, alpha=.9, data=df, cmap="Set1", edgecolors='black', linewidths=.5)


# Add a graph in each part

sns.boxplot(df.hwy, ax=ax_right, orient="v")

sns.boxplot(df.displ, ax=ax_bottom, orient="h")


# Decorations ------------------

# Remove x axis name for the boxplot

ax_bottom.set(xlabel='')

ax_right.set(ylabel='')


# Main Title, Xlabel and YLabel

ax_main.set(title='Scatterplot with Histograms
displ vs hwy'
, xlabel='displ', ylabel='hwy')


# Set font size of different components

ax_main.title.set_fontsize(20)

for item in ([ax_main.xaxis.label, ax_main.yaxis.label] + ax_main.get_xticklabels() + ax_main.get_yticklabels()):

   item.set_fontsize(14)


plt.show()



29.jpg

8. 相关图

Correlogram用于直观地查看给定数据帧(或2D数组)中所有可能的数值变量对之间的相关度量。

# Import Dataset

df = pd.read_csv("https://github.com/selva86/datasets/raw/master/mtcars.csv")


# Plot

plt.figure(figsize=(12,10), dpi= 80)

sns.heatmap(df.corr(), xticklabels=df.corr().columns, yticklabels=df.corr().columns, cmap='RdYlGn', center=0, annot=True)


# Decorations

plt.title('Correlogram of mtcars', fontsize=22)

plt.xticks(fontsize=12)

plt.yticks(fontsize=12)

plt.show()

30.jpg

9. 矩阵图

成对图是探索性分析中的最爱,以理解所有可能的数字变量对之间的关系。它是双变量分析的必备工具。

# Load Dataset

df = sns.load_dataset('iris')


# Plot

plt.figure(figsize=(10,8), dpi= 80)

sns.pairplot(df, kind="scatter", hue="species", plot_kws=dict(s=80, edgecolor="white", linewidth=2.5))

plt.show()


31.jpg


# Load Dataset

df = sns.load_dataset('iris')


# Plot

plt.figure(figsize=(10,8), dpi= 80)

sns.pairplot(df, kind="reg", hue="species")

plt.show()

32.jpg

相关文章
|
5天前
|
并行计算 C语言 开发者
优化Python代码的五大技巧
Python作为一种流行的编程语言,在各种应用场景中广泛使用。然而,随着项目规模的增长和需求的变化,Python代码的性能和可维护性也成为了关键问题。本文将介绍优化Python代码的五大技巧,帮助开发者提升代码效率和质量。
|
15天前
|
监控 Python
Python中的装饰器:提升代码灵活性与可读性
在Python编程中,装饰器是一种强大的工具,能够提升代码的灵活性和可读性。本文将介绍装饰器的基本概念、使用方法以及实际应用场景,帮助读者更好地理解和利用这一功能。
|
17天前
|
人工智能 数据可视化 数据挖掘
【python】Python航空公司客户价值数据分析(代码+论文)【独一无二】
【python】Python航空公司客户价值数据分析(代码+论文)【独一无二】
|
21天前
|
机器学习/深度学习 算法 搜索推荐
Machine Learning机器学习之决策树算法 Decision Tree(附Python代码)
Machine Learning机器学习之决策树算法 Decision Tree(附Python代码)
|
16天前
|
缓存 监控 算法
优化Python代码性能的10个技巧
提高Python代码性能是每个开发者都需要关注的重要问题。本文将介绍10个实用的技巧,帮助你优化Python代码,提升程序的运行效率和性能表现。无论是避免内存泄漏、减少函数调用次数,还是使用适当的数据结构,都能在不同场景下发挥作用,使你的Python应用更加高效稳定。
|
1天前
|
人工智能 Python
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
15 0
|
1天前
|
人工智能 Python
【AI大模型应用开发】【LangChain系列】实战案例1:用LangChain写Python代码并执行来生成答案
【AI大模型应用开发】【LangChain系列】实战案例1:用LangChain写Python代码并执行来生成答案
4 0
|
2天前
|
数据安全/隐私保护 Python
Python中的装饰器:提升代码可读性和灵活性
Python中的装饰器是一种强大的编程工具,能够提升代码的可读性和灵活性。本文将深入探讨装饰器的原理和用法,以及如何利用装饰器来简化代码、实现日志记录、权限控制等功能,从而让你的Python代码更加优雅和高效。
|
6天前
|
数据可视化 数据挖掘 定位技术
Python 基于 Matplotlib 实现数据可视化(二)
Python 基于 Matplotlib 实现数据可视化(二)
19 0
|
7天前
|
数据可视化 数据挖掘 Python
Python中数据分析工具Matplotlib
【4月更文挑战第14天】Matplotlib是Python的数据可视化库,能生成多种图表,如折线图、柱状图等。以下是一个绘制简单折线图的代码示例: ```python import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] plt.figure() plt.plot(x, y) plt.title('简单折线图') plt.xlabel('X轴') plt.ylabel('Y轴') plt.show() ```
12 1

热门文章

最新文章