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教程到此结束。

目录
相关文章
|
12天前
|
数据可视化 Linux API
利用Matplotlib和Seaborn进行数据可视化与探索性分析
本文探讨了Python中数据可视化的关键工具——Matplotlib和Seaborn。Matplotlib是基础绘图库,可用于创建折线图,通过自定义参数实现图表美化。Seaborn是建立在Matplotlib之上的库,专注于统计图形,提供箱线图、散点图等,并有内置数据集便于学习。通过案例展示了如何利用这两个库分析销售数据,包括散点图、分布图和类别对比图,以助于数据探索和理解。
19 6
|
10天前
|
数据可视化 Python Windows
使用Python进行数据可视化(一、matplotlib)
使用Python进行数据可视化(一、matplotlib)
|
15天前
|
数据可视化 Python
利用Matplotlib绘制数据可视化图表
**摘要:** 本文介绍了Python的绘图库Matplotlib在数据分析和科学计算中的重要性。Matplotlib是一个开源库,提供类似MATLAB的接口,支持静态、动态和交互式图表的绘制,并能保存为多种格式。文章详细讲解了Matplotlib的基本用法,包括安装库、导入模块和绘制简单折线图的步骤。还展示了如何绘制柱状图并添加数据标签。通过这些例子,读者可以了解如何利用Matplotlib进行数据可视化,并对其进行自定义以满足特定需求。
22 4
|
15天前
|
数据可视化 数据挖掘 大数据
Python中的数据可视化库Matplotlib及其应用
数据可视化是数据分析过程中至关重要的一环,而Matplotlib作为Python中最流行的数据可视化库之一,为用户提供了丰富的绘图工具和定制选项。本文将介绍Matplotlib的基本用法和常见应用,帮助读者更好地利用这一强大工具进行数据呈现和分析。
|
18天前
|
数据可视化 Linux Python
20. Matplotlib 数据可视化(下)
20. Matplotlib 数据可视化
15 1
|
2天前
|
数据可视化 数据挖掘 API
Python数据可视化基础:使用Matplotlib绘制图表
Python的Matplotlib是数据可视化的首选库,它提供静态、动态和交互式图表。要开始,先通过`pip install matplotlib`安装。绘制基本折线图涉及导入`pyplot`,设定数据,然后用`plot()`函数画图,如: ```markdown import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] plt.plot(x, y, 'o') plt.show() ``` 自定义图表包括更改线条样式、颜色等,例如: ```markdown
|
9天前
|
Python
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)-2
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)
|
9天前
|
数据可视化 开发者 Python
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)-1
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)
|
13天前
|
人工智能 数据可视化 开发者
|
1月前
|
数据可视化 BI 索引
【Python】—— matplotlib数据可视化
【Python】—— matplotlib数据可视化
27 1