Python中的 Matplotlib.pyplot.broken_barh()
Matplotlib是用于数据可视化的最流行的Python包之一。它是一个跨平台库,用于从数组中的数据制作二维图。 Pyplot是使 matplotlib 像 MATLAB 一样工作的命令样式函数的集合。
matplotlib.pyplot.broken_barh()
函数broken_barh()用于绘制矩形的水平序列。为由元组序列组成的 xranges 的每个组件绘制一个矩形。所有矩形都具有相同的垂直位置和以 yrange 为特征的估计值。
Syntax: matplotlib.pyplot.broken_barh(xranges, yrange, *, data=None, **kwargs)
Parameters:
- xranges : sequence of tuples (xmin, xwidth)
Each tuples gives the position(xmin) of the rectangle and it’s horizontal extension(xwidth) from that position. - yranges : (ymin, ymax)
In the above attribute, ymin gives the position of the rectangle and ymax gives the vertical extension from ymin.
Returns:
- BrokenBarHCollection: A collection of horizontal bars spanning yrange with a sequence of xranges.
说明 matplotlib.pyplot.broken_barh()函数的示例如下:
示例 1:
# importing module
import matplotlib.pyplot as plt
# Adding title to the plot
plt.title('GEEKSFORGEEKS - EXAMPLE')
# adding x axis label to the plot
plt.xlabel('x-label')
# label for y axis for the plot
plt.ylabel('y-label')
x_1 = [(1, 4), (10, 7)]
y_1 = (2, 2)
# Plotting the chart
plt.broken_barh(x_1, y_1, facecolors ='green')
x_2 = [(10, 1), (15, 4), (25, 6)]
y_2 = (6, 2)
# Plotting the chart
plt.broken_barh(x_2, y_2, facecolors ='cyan')
plt.show()
输出 :
示例 2:
# importing module
import matplotlib.pyplot as plt
# Adding title to the plot
plt.title('GEEKSFORGEEKS - EXAMPLE')
# adding x axis label to the plot
plt.xlabel('Number of Cars')
# label for y axis for the plot
plt.ylabel('Average Speed')
x_1 = [(10, 3), (15, 4)]
y_1 = (50, 10)
# Plotting the chart
plt.broken_barh(x_1, y_1, facecolors ='cyan')
x_2 = [(1, 4), (10, 1), (15, 4), (25, 6)]
y_2 = (70, 10)
# Plotting the chart
plt.broken_barh(x_2, y_2, facecolors ='green')
x_3 = [(5, 3), (11, 2), (18, 5)]
y_3 = (90, 10)
# Plotting the chart
plt.broken_barh(x_3, y_3, facecolors ='blue')
plt.show()
输出 :