在一张图中绘制多个函数
在某些情况下,出于特殊目的,用户可能必须在单个图像中显示多个函数。例如,零售商想知道最近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')
请输入图片描述
请输入图片描述
# 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')
请输入图片描述
创建子图
在某些情况下,如果我们要给股东汇报公司最近的情况,我们需要在一个图中显示多个子图。这可以通过使用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)
请输入图片描述
请输入图片描述
# 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')
请输入图片描述
请输入图片描述
# 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')
请输入图片描述
请输入图片描述
# 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')
请输入图片描述
请输入图片描述
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)
请输入图片描述
请输入图片描述
# 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)
请输入图片描述
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()
请输入图片描述
请输入图片描述
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()
请输入图片描述
#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)
请输入图片描述
# 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))
请输入图片描述
# to plot horizontal bar plot use plt.barh() function plt.barh(subject,marks,color ='g',xerr=np.std(marks))
请输入图片描述