10分钟入门Matplotlib: 数据可视化介绍&使用教程(三)

简介: 10分钟入门Matplotlib: 数据可视化介绍&使用教程(三)

饼状图

概述:

饼状图表示每个值相对于所有值之和的比例。饼状图上的值以扇形的形式显示了每个值的百分比贡献。扇形的角度是根据值的比例计算的。当我们试图比较总体中的不同部分时,这种可视化效果是最好的。例如,一个销售经理想要知道一个月里不同付款类型所占比例,如现金、信用卡、借记卡、PayPal等应用的支付比例。

函数:


用于饼状图的函数是' plt.pie() '


为了绘制饼状图,我们需要输入一个列表,每个扇形都是先计算列表中的值所占比例,再转换成角度得到的


自定义:

plt.pie()函数具有以下参数,可用于配置绘图。


labels – 用于显示每个扇形所属的类别


explode – 用于突出扇形


autopct –用于显示扇形区域所占百分比


shadow –在扇形上显示阴影


colours –为扇形设置自定义颜色


startangle –设置扇形的角度


例子:

# Let’s create a simple pie plot
# Assume that we have a data on number of tickets resolved in a month
# the manager would like to know the individual contribution in terms of tickets closed in the week
# data
Tickets_Closed =
Agents =
# create pie chart
plt.pie(Tickets_Closed, labels = Agents)

image.png

请输入图片描述


image.png

请输入图片描述

#Let’s add additional parameters to pie plot
#explode – to move one of the wedges of the plot
#autopct – to add the contribution %
explode =
plt.pie(Tickets_Closed, labels = Agents, explode=explode, autopct='%1.1f%%' )


image.png

请输入图片描述


image.png

请输入图片描述


散点图

概述:

散点图通过显示数据点来展示两列数据之间的关系。绘制散点图需要两个变量,一个变量表示X轴位置,另一个变量表示y轴位置。散点图用于表示变量之间的关联,通常建议在进行回归之前使用。散点图有助于理解数据的以下信息:


两列数据间的任何关系


+ ve(阳性)关系


-ve(阴性)关系


函数:


用于散点图的函数是“pl .scatter()”


自定义:

scatter()函数具有以下参数,用于配置绘图。


size – 设置点的大小


color –设置点的颜色


marker – 标记的类型


alpha – 点的透明度


norm –规范化数据(将数据归一化0至1)


例子:

# let's create a  simple scatter plot
# generate the data with random numbers
x = np.random.randn(1000)
y = np.random.randn(1000)
plt.scatter(x,y)


image.png

请输入图片描述


image.png

请输入图片描述


# as you observe there is no correlation exists between x and y
# let’s try to add additional parameters
# size – to manage the size of the points
#color – to set the color of the points
#marker – type of marker
#alpha – transparency of point
size = 150*np.random.randn(1000)
colors = 100*np.random.randn(1000)
plt.scatter(x, y, s=size, c = colors, marker ='*', alpha=0.7)


image.png

请输入图片描述


image.png

请输入图片描述


直方图

概述:

直方图是用来了解数据分布的。它是对连续数据概率分布的估计。它与上面讨论的条形图相似,但它用于表示连续变量的分布,而条形图用于表示离散变量的分布。每个分布都有四个不同的特征,包括


分布中心


分布散布


分布形状


分布峰值


直方图需要两个输入,x轴表示bin, y轴表示数据集中每个bin对应值的频率。每个bin都有一个最小值和最大值的范围。

函数:


绘制直方图使用的函数是“plt.hist()”


自定义:

函数的具体参数如下,可用于配置绘图:


bins – bin的个数


color-颜色


edgecolor-边缘的颜色


alpha – 颜色透明度


normed –正则化


xlim – X轴范围


ylim –Y轴范围


xticks, yticks-坐标轴的刻度


facecolor-柱的颜色


例子:

# let’s generate random numbers and use the random numbers to generate histogram
data = np.random.randn(1000)
plt.hist(data)


image.png

请输入图片描述


image.png

请输入图片描述


# let’s add additional parameters
# facecolor
# alpha
# edgecolor
# bins
data = np.random.randn(1000)
plt.hist(data, facecolor ='y',linewidth=2,edgecolor='k', bins=30, alpha=0.6)

image.png

请输入图片描述


image.png

请输入图片描述


# lets create multiple histograms in a single plot
# Create random data
hist1 = np.random.normal(25,10,1000)
hist2 = np.random.normal(200,5,1000)
#plot the histogram
plt.hist(hist1,facecolor = 'yellow',alpha = 0.5, edgecolor ='b',bins=50)
plt.hist(hist2,facecolor = 'orange',alpha = 0.8, edgecolor ='b',bins=30)

image.png

请输入图片描述

image.png

请输入图片描述


保存绘图

使用matplotlib中的“savefig()”函数可将图保存到本地。图可以以多种格式保存,如.png、.jpeg、.pdf以及其他支持的格式。

# let's create a figure and save it as image
items =
x = np.arange(6)
fig = plt.figure()
ax = plt.subplot(111)
ax.plot(x, y, label='items')
plt.title('Saving as Image')
ax.legend()
fig.savefig('saveimage.png')


image.png

请输入图片描述


图像以“saveimage.png”为文件名保存。

#To display the image again, use the following package and commands
import matplotlib.image as mpimg
image = mpimg.imread("saveimage.png")
plt.imshow(image)
plt.show()


image.png

请输入图片描述


Matplotlib教程到此结束。

目录
相关文章
|
2月前
|
数据可视化 数据挖掘 开发者
Pandas数据可视化:matplotlib集成(df)
Pandas 是 Python 中强大的数据分析库,Matplotlib 是常用的绘图工具。两者结合可方便地进行数据可视化,帮助理解数据特征和趋势。本文从基础介绍如何在 Pandas 中集成 Matplotlib 绘制图表,如折线图、柱状图等,并深入探讨常见问题及解决方案,包括图表显示不完整、乱码、比例不合适、多子图布局混乱、动态更新图表等问题,提供实用技巧和代码示例。掌握这些方法后,你将能更高效地处理数据可视化任务。
59 9
|
3月前
|
机器学习/深度学习 计算机视觉 Python
Matplotlib 教程
Matplotlib 教程
37 1
|
3月前
|
移动开发 数据可视化 数据挖掘
利用Python实现数据可视化:以Matplotlib和Seaborn为例
【10月更文挑战第37天】本文旨在引导读者理解并掌握使用Python进行数据可视化的基本方法。通过深入浅出的介绍,我们将探索如何使用两个流行的库——Matplotlib和Seaborn,来创建引人入胜的图表。文章将通过具体示例展示如何从简单的图表开始,逐步过渡到更复杂的可视化技术,帮助初学者构建起强大的数据呈现能力。
|
4月前
|
数据可视化 Python
Matplotlib 教程 之 Seaborn 教程 10
Seaborn 是基于 Matplotlib 的 Python 数据可视化库,专注于统计图形的绘制。它提供了高级接口和美观的默认主题,简化了复杂图形的生成过程。Seaborn 支持多种图表类型,如散点图、折线图、柱状图、热图等,并特别强调视觉效果。例如,使用 `sns.violinplot()` 可以轻松绘制展示数据分布的小提琴图。
46 1
|
4月前
|
数据可视化 Python
Matplotlib 教程 之 Seaborn 教程 9
Seaborn 是基于 Matplotlib 的 Python 数据可视化库,专注于统计图形的绘制。它提供了高级接口和美观的默认主题,简化了复杂图形的生成过程。本文介绍了 Seaborn 的主要功能和绘图函数,包括热图 `sns.heatmap()` 的使用方法和示例代码。
31 1
|
4月前
|
数据可视化 数据挖掘 Python
Matplotlib 教程 之 Seaborn 教程 8
Seaborn 是基于 Matplotlib 的 Python 数据可视化库,专注于统计图形的绘制。它提供了简洁的高级接口和美观的默认样式,支持多种图表类型,如散点图、折线图、柱状图、热图等,特别适合于数据分析和展示。例如,使用 `sns.boxplot()` 可以轻松绘制箱线图,展示数据的分布情况。
50 3
|
4月前
|
数据可视化 DataX Python
Matplotlib 教程 之 Seaborn 教程 6
Seaborn 是基于 Matplotlib 的 Python 数据可视化库,专注于绘制统计图形。它提供高级接口和美观的默认主题,简化了复杂图形的绘制过程。本文档介绍了 Seaborn 的主要绘图函数,如 `sns.lineplot()` 用于绘制变量变化趋势的折线图,并给出了示例代码。
49 0
|
4月前
|
数据可视化 Python
Matplotlib 教程 之 Seaborn 教程 4
Seaborn 是基于 Matplotlib 的 Python 数据可视化库,专注于绘制统计图形。它提供了高级接口和美观的默认主题,简化了复杂图形的绘制过程。以下示例展示了如何使用 Seaborn 和 Matplotlib 绘制一个简单的柱状图,展示不同产品的销售情况。
27 0
|
4月前
|
数据可视化 定位技术 Python
Python数据可视化--Matplotlib--入门
Python数据可视化--Matplotlib--入门
42 0
|
4月前
|
数据可视化 Python
Matplotlib 教程 之 Seaborn 教程 2
Seaborn 是基于 Matplotlib 的 Python 数据可视化库,专注于统计图形的绘制,提供高级接口和美观的默认主题,支持散点图、折线图等多种图表类型,安装简便,可通过 `pip install seaborn` 完成。Seaborn 设计注重美观与易用性,内置多种主题如 darkgrid、whitegrid 等,便于用户快速生成高质量的统计图表。
42 3