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

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

在一张图中绘制多个函数

在某些情况下,出于特殊目的,用户可能必须在单个图像中显示多个函数。例如,零售商想知道最近12个月中两家分店的销售趋势,而他希望在同一个坐标轴里查看两家商店的趋势。

让我们在一张图中绘制两条线sin(x)和cos(x),并添加图例以了解哪一条线是什么。

# lets plot two lines Sin(x) and Cos(x)
# loc is used to set the location of the legend on the plot
# label is used to represent the label for the line in the legend
# generate the random number
x= np.arange(0,1500,100)
plt.plot(np.sin(x),label='sin function x')
plt.plot(np.cos(x),label='cos functon x')
plt.legend(loc='upper right')


image.png

请输入图片描述


image.png

请输入图片描述

# To show the multiple plots in separate figure instead of a single figure, use plt.show() statement before the next plot statement as shown below
x= np.linspace(0,100,50)
plt.plot(x,'r',label='simple x')
plt.show()
plt.plot(x*x,'g',label='two times x')
plt.show()
plt.legend(loc='upper right')


image.png

请输入图片描述


创建子图

在某些情况下,如果我们要给股东汇报公司最近的情况,我们需要在一个图中显示多个子图。这可以通过使用matplotlib库中的subplot来实现。例如,一家零售店有6家分店,经理希望在一个展示窗口中看到6家商店的每日销售额并进行比较。这可以通过subplots将报表的行和列进行可视化处理。

# subplots are used to create multiple plots in a single figure
# let’s create a single subplot first following by adding more subplots
x = np.random.rand(50)
y = np.sin(x*2)
#need to create an empty figure with an axis as below, figure and axis are two separate objects in matplotlib
fig, ax = plt.subplots()
#add the charts to the plot
ax.plot(y)


image.png

请输入图片描述

image.png

请输入图片描述

# Let’s add multiple plots using subplots() function
# Give the required number of plots as an argument in subplots(), below function creates 2 subplots
fig, axs = plt.subplots(2)
#create data
x=np.linspace(0,100,10)
# assign the data to the plot using axs
axs.plot(x, np.sin(x**2))
axs.plot(x, np.cos(x**2))
# add a title to the subplot figure
fig.suptitle('Vertically stacked subplots')

image.png

请输入图片描述


image.png

请输入图片描述

# Create horizontal subplots
# Give two arguments rows and columns in the subplot() function
# subplot() gives two dimensional array with 2*2 matrix
# need to provide ax also similar 2*2 matrix as below
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
# add the data to the plots
ax1.plot(x, x**2)
ax2.plot(x, x**3)
ax3.plot(x, np.sin(x**2))
ax4.plot(x, np.cos(x**2))
# add title
fig.suptitle('Horizontal plots')


image.png

请输入图片描述

image.png

请输入图片描述


# another simple way of creating multiple subplots as below, using axs
fig, axs = plt.subplots(2, 2)
# add the data referring to row and column
axs.plot(x, x**2,'g')
axs.plot(x, x**3,'r')
axs.plot(x, np.sin(x**2),'b')
axs.plot(x, np.cos(x**2),'k')
# add title
fig.suptitle('matrix sub plots')


image.png

请输入图片描述


image.png

请输入图片描述

Figure对象

Matplotlib是一个面向对象的库,包括对象、方法等。我们所绘制的图也是Figure对象中的类之一。Figure对象是用于显示图的容器,可通过调用Figure()函数实现。


Figsize – (宽, 高)英寸


Dpi –用于调整每英寸网点数(可根据打印质量进行调整)


facecolor


edgecolor


linewidth


# let’s create a figure object
# change the size of the figure is ‘figsize = (a,b)’ a is width and ‘b’ is height in inches
# create a figure object and name it as fig
fig = plt.figure(figsize=(4,3))
# create a sample data
X = np.array()
Y = X**2
# plot the figure
plt.plot(X,Y)


image.png

请输入图片描述

image.png

请输入图片描述

# let’s change the figure size and also add additional parameters like facecolor, edgecolor, linewidth
fig = plt.figure(figsize=(10,3),facecolor='y',edgecolor='r',linewidth=5)


image.png

请输入图片描述


Axes对象

Axes是指绘制数据的区域,我们可以使用' add_axes() '将Axes添加到图中。该方法需要以下四个参数:,Left,Bottom,Width,Height


Left–Axes与图中左侧的距离


Bottom–Axes与图中底部的距离


Width–Axes的宽度


Height–Axes的高度


Axes的其他属性:


Set title using ‘ax.set_title()’


Set x-label using ‘ax.set_xlabel()’


Set y-label using ‘ax.set_ylabel()’

# lets add axes using add_axes() method
# create a sample data
y =
x1 =
x2 =
# create the figure
fig = plt.figure()
# add the axes
ax = fig.add_axes()
l1 = ax.plot(x1,y,'ys-')
l2 = ax.plot(x2,y,'go--')
# add additional parameters
ax.legend(labels = ('line 1', 'line 2'), loc = 'lower right')
ax.set_title("usage of add axes function")
ax.set_xlabel('x-axix')
ax.set_ylabel('y-axis')
plt.show()

image.png

请输入图片描述


image.png

请输入图片描述

Matplotlib中的绘图类型

Matplotlib有各种各样的绘图类型,包括条形图、折线图、饼状图、散点图、气泡图、瀑布图、圆形区域图、堆叠条形图等,我们将通过一些例子来介绍它们。这些图的许多属性都是通用的,如axis, color等,但有些属性却是特有的。

条形图

概述:

条形图使用水平或垂直方向的长条去表示数据。条形图用于显示两个或多个类别的值,通常x轴代表类别。每个长条的长度与对应类别的计数成正比。

函数:


用于显示条形图的函数是' plt .bar() '


bar()函数需要输入X轴和Y轴的数据


自定义:

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


Width, Color, edge colour, line width, tick_label, align, bottom,


Error Bars – xerr, yerr


# lets create a simple bar chart
# x-axis is shows the subject and y -axis shows the markers in each subject
例子:
subject =
marks =
plt.bar(subject,marks)
plt.show()

image.png

请输入图片描述


#let’s do some customizations
#width – shows the bar width and default value is 0.8
#color – shows the bar color
#bottom – value from where the y – axis starts in the chart i.e., the lowest value on y-axis shown
#align – to move the position of x-label, has two options ‘edge’ or ‘center’
#edgecolor – used to color the borders of the bar
#linewidth – used to adjust the width of the line around the bar
#tick_label – to set the customized labels for the x-axis
plt.bar(subject,marks,color ='g',width = 0.5,bottom=10,align ='center',edgecolor='r',linewidth=2,tick_label=subject)

image.png

请输入图片描述


# errors bars could be added to represent the error values referring to an array value
# here in this example we used standard deviation to show as error bars
plt.bar(subject,marks,color ='g',yerr=np.std(marks))


image.png

请输入图片描述


# to plot horizontal bar plot use plt.barh() function
plt.barh(subject,marks,color ='g',xerr=np.std(marks))


image.png

请输入图片描述

目录
相关文章
|
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