Python中的 Matplotlib.pyplot.stackplot()
Matplotlib是Python中可用的可视化库。 Pyplot包含各种帮助 matplotlib 表现得像 MATLAB 的函数。它用作 matplotlib.pyplot 用于绘制图形、创建区域、线条等。
堆栈图
在 pyplot 提供的众多功能中,stackplot 将在本文中讨论。 Stackplot 用于绘制堆积面积图。它显示完整的可视化数据。它显示了堆叠在一起的每个部分以及每个部分如何构成完整的图形。它显示数据的各种组成部分,其行为类似于饼图。它具有 x-label、y-label 和 title,其中各个部分可以用不同的颜色表示。
堆栈图的想法是随着时间的推移显示“部分到整体”。它用于表示各种数据集,而不会相互重叠。Parameter Value Use x 1-D Array It is 1 D array with N Dimensions used to give values to X-axis y 2-D array Represents 2 D array of M*N Dimension which is unstacked. Colors Contains List or tuple of Colors It is used to give range of colors to represent data with default value is None. Baseline {‘zero’, ‘sym’, ‘wiggle’, ‘weighted_wiggle’} Zero means constant baseline.
Sym which is symmetric around zero value.
wiggle it will minimize value of the sum of squares.
**kwargs List of other keywords Other Arguments or keywords.
句法:
matplotlib.pyplot.stackplot(x, *args, labels=(), colors=None, baseline=’zero’, data=None, **kwargs)
示例 #1 :使用 Stackplot
该代码将 x 轴描述为从星期一到星期五的天数,而 Y 轴由 No of Study 表示,播放时间分别由红色和青色表示。
Python3
import matplotlib.pyplot as plt
# List of Days
days = [1, 2, 3, 4, 5]
# No of Study Hours
Studying = [7, 8, 6, 11, 7]
# No of Playing Hours
playing = [8, 5, 7, 8, 13]
# Stackplot with X, Y, colors value
plt.stackplot(days, Studying, playing,
colors =['r', 'c'])
# Days
plt.xlabel('Days')
# No of hours
plt.ylabel('No of Hours')
# Title of Graph
plt.title('Representation of Study and \
Playing wrt to Days')
# Displaying Graph
plt.show()
Python3
import matplotlib.pyplot as plt
# List of 7-days
days = [x for x in range(0, 7)]
# List of Suspected cases
Suspected = [12, 18, 35, 50, 72, 90, 100]
# List of Cured Cases
Cured = [4, 8, 15, 22, 41, 55, 62]
# List of Number of deaths
Deaths = [1, 3, 5, 7, 9, 11, 13]
# Plot x-labels, y-label and data
plt.plot([], [], color ='blue',
label ='Suspected')
plt.plot([], [], color ='orange',
label ='Cured')
plt.plot([], [], color ='brown',
label ='Deaths')
# Implementing stackplot on data
plt.stackplot(days, Suspected, Cured,
Deaths, baseline ='zero',
colors =['blue', 'orange',
'brown'])
plt.legend()
plt.title('No of Cases')
plt.xlabel('Day of the week')
plt.ylabel('Overall cases')
plt.show()
输出:
示例 #2 :使用 Stackplot
Python3
import matplotlib.pyplot as plt
# List of 7-days
days = [x for x in range(0, 7)]
# List of Suspected cases
Suspected = [12, 18, 35, 50, 72, 90, 100]
# List of Cured Cases
Cured = [4, 8, 15, 22, 41, 55, 62]
# List of Number of deaths
Deaths = [1, 3, 5, 7, 9, 11, 13]
# Plot x-labels, y-label and data
plt.plot([], [], color ='blue',
label ='Suspected')
plt.plot([], [], color ='orange',
label ='Cured')
plt.plot([], [], color ='brown',
label ='Deaths')
# Implementing stackplot on data
plt.stackplot(days, Suspected, Cured,
Deaths, baseline ='zero',
colors =['blue', 'orange',
'brown'])
plt.legend()
plt.title('No of Cases')
plt.xlabel('Day of the week')
plt.ylabel('Overall cases')
plt.show()
输出:
如果baseline的值设置为零,下面表示图形的输出
下面表示如果baseline的值设置为sym,graph的输出
下面表示如果baseline的值设置为wiggle,graph的输出
下面表示将baseline的值设置为weighted_wiggle时的graph输出