Matplotlib 简明教程

Matplotlib - Ribbon Box

一般来说,“带状框”是一个图形表述,用于可视化不同类别或组中数据分布。术语“带状框”指的是以直观且信息丰富的方式显示这些分布的特定绘图类型。当您有许多类别或组并且想要跨这些类别比较变量分布时,它特别有用。

In general, a "ribbon box" is a graphical representation used to visualize data distributions across different categories or groups. The term "ribbon box" refers to a specific type of plot that displays these distributions in a visually appealing and informative manner. It is particularly useful when you have multiple categories or groups and want to compare the distributions of a variable across these categories.

在带状框图中:

In a ribbon box plot −

  1. Each category or group is typically represented along the x-axis.

  2. The y-axis often represents the range or distribution of a numeric variable.

  3. Each "ribbon" corresponds to the distribution of the numeric variable within a particular category or group.

可以对带进行着色或着色以指示每个类别内分布的密度或强度。这样可以轻松比较各类别之间的分布。

The ribbon can be shaded or colored to indicate the density or intensity of the distribution within each category. This allows for easy comparison of distributions across different categories.

ribbon box1

在此,我们创建了一个简单的带状框图,其中 x 轴上有三个类别(类别 1、类别 2、类别 3)及其沿 y 轴的值分配。我们对带状框着色以指示每个类别内的分布强度。

Here, we created a simple ribbon box plot with three categories (Category 1, Category 2, Category 3) along the x-axis and their corresponding distribution of values along the y-axis. We shade the ribbon boxes to indicate the intensity of the distribution within each category.

Ribbon Box in Matplotlib

在 Matplotlib 中,“带状框”是一个视觉表述,用于显示不同类别或组中数字变量的分布。虽然 matplotlib 没有用于创建带状框图的特定函数,但您可以使用其他可用技术,例如:

In Matplotlib, a "ribbon box" is a visual representation used to display the distribution of a numeric variable across different categories or groups. While matplotlib does not have a specific function to create ribbon box plots, you can use other techniques available, such as −

  1. Use matplotlib’s plot() function to plot the central line for each category or group. This line represents the central tendency of the data within each category, such as the mean or median.

  2. Using the fill_between function to fill the area between two curves, where one curve represents the upper boundary of the ribbon and the other curve represents the lower boundary.

  3. Customize the appearance of the plot as needed by adding labels, titles, legends, gridlines, etc.

Ribbon Box Plot with Confidence Interval

在 matplotlib 中,带置信区间的简单带状框图是一个图形表述,用于显示数据集的中心趋势以及围绕中心值的模糊性。

In matplotlib, a simple ribbon box plot with confidence interval is a graphical representation used to display the central tendency of a dataset along with the uncertainty around that central value.

它就像绘制某个东西(比如每日气温)的平均值,然后在其周围进行阴影处理以显示实际值可能会在不确定性下如何变化。

It is like plotting the average of something (like daily temperatures) and shading an area around it to show how much the actual values might vary due to uncertainty.

Example

在以下示例中,我们正在创建一个带状盒图,使用 matplotlib 的 plot() 和 fill_between() 函数分别显示中心趋势和正弦波周围的置信区间(不确定性)。

In the following example, we are creating a ribbon box plot showing the central tendency and confidence interval (uncertainity) around a sine wave, using matplotlib’s plot() and fill_between() functions, respectively −

import matplotlib.pyplot as plt
import numpy as np

# Generating data
x = np.linspace(0, 10, 100)
y_mean = np.sin(x)
# Standard deviation
y_std = 0.1

# Plotting the central line
plt.plot(x, y_mean, color='blue', label='Mean')

# Plotting the shaded area representing the uncertainty (confidence interval)
plt.fill_between(x, y_mean - y_std, y_mean + y_std, color='blue', alpha=0.2, label='Uncertainty')

plt.xlabel('X')
plt.ylabel('Y')
plt.title('Simple Ribbon Box Plot with Confidence Interval')
plt.legend()
plt.grid(True)
plt.show()

以下是上面代码的输出: -

Following is the output of the above code −

ribbon box2

Multiple Ribbon Box Plots

在 Matplotlib 中,多带状盒图是一种使用同一个图中的带状盒图来比较多个数据集分布的方法。每个带状盒图都表示一个不同数据集的扩展和中心趋势,可以轻松地比较它们。

In Matplotlib, multiple ribbon box plots are a way to compare the distributions of multiple datasets using ribbon box plots within the same plot. Each ribbon box plot represents the spread and central tendency of a different dataset, allowing for easy comparison between them.

Example

在此,我们使用 matplotlib 生成多个不同颜色的带状盒图来表示两个正弦和余弦波及其不确定性带:

In here, we are generating multiple ribbon box plots with different colors to represent two sine and cosine waves along with their uncertainty bands using matplotlib −

import matplotlib.pyplot as plt
import numpy as np

# Generating data
x = np.linspace(0, 10, 100)
y_means = [np.sin(x), np.cos(x)]
# Standard deviations
y_stds = [0.1, 0.15]
colors = ['blue', 'green']

# Plotting multiple ribbon box plots with different colors
for y_mean, y_std, color in zip(y_means, y_stds, colors):
   plt.plot(x, y_mean, color=color, label='Mean', alpha=0.7)
   plt.fill_between(x, y_mean - y_std, y_mean + y_std, color=color, alpha=0.2)

plt.xlabel('X')
plt.ylabel('Y')
plt.title('Multiple Ribbon Box Plots with Different Colors')
plt.legend()
plt.grid(True)
plt.show()

执行上述代码,我们将得到以下输出 −

On executing the above code we will get the following output −

ribbon box3

Stacked Ribbon Box Plot

在 Matplotlib 中,堆叠带状盒图是一种用于比较多个数据集分布的同时还能显示所有这些数据集组合分布的图形表示类型。

In Matplotlib, stacked ribbon box plots are a type of graphical representation used to compare the distributions of multiple datasets while also showing the combined distribution of all the datasets.

在堆叠带状盒图中,每个数据集都由它自己的带状盒图表示,就像在多个带状盒图中一样。但是,它不是并排显示盒图,而是垂直堆叠在彼此的顶部。这种堆叠允许对每个分布的分布进行直接比较,同时也可以显示它们在组合时如何对总体分布做出贡献。

In a stacked ribbon box plot, each dataset is represented by its own ribbon box plot, just like in multiple ribbon box plots. However, instead of displaying the box plots side by side, they are stacked vertically on top of each other. This stacking allows for a direct comparison of the distributions of each dataset while also showing how they contribute to the overall distribution when combined.

Example

现在,我们正在绘制一个堆叠带状盒图,堆叠正弦和余弦波的分布以使用 matplotlib 比较它们在 x 轴上的变化情况:

Now, we are plotting a stacked ribbon box plot, stacking the distributions of a sine and cosine wave to compare their variations across the x-axis using matplotlib −

import matplotlib.pyplot as plt
import numpy as np

# Generating example data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

# Plotting stacked ribbon box plot
plt.plot(x, y1, color='blue', label='Dataset 1')
plt.fill_between(x, y1, color='blue', alpha=0.2)
plt.plot(x, y2, color='green', label='Dataset 2')
plt.fill_between(x, y2, color='green', alpha=0.2)

plt.xlabel('X')
plt.ylabel('Y')
plt.title('Stacked Ribbon Box Plot')
plt.legend()
plt.grid(True)
plt.show()

执行上面的代码后,我们得到以下输出: -

After executing the above code, we get the following output −

ribbon box4

Horizontal Ribbon Box Plot

Matplotlib 中的水平带状盒图是一种图形表示,它使用带状盒子沿着水平轴显示数据集的分布。

A horizontal ribbon box plot in Matplotlib is a graphical representation that shows the distribution of a dataset along a horizontal axis using ribbon-shaped boxes.

在水平带状盒图中,将数据集的值分组到类别或箱中,并且为每个类别水平绘制带状盒子。每个盒子的长度表示该类别中值的范围,而沿水平轴的位置表示类别本身。

In a horizontal ribbon box plot, the dataset’s values are grouped into categories or bins, and for each category, a ribbon-shaped box is drawn horizontally. The length of each box represents the range of values within that category, while the position along the horizontal axis indicates the category itself.

Example

在以下示例中,我们正在创建一个水平带状盒图来表示沿 y 轴的类别及其使用 matplotlib 的相应平均值和不确定性:

In the following example, we are creating a horizontal ribbon box plot to represent categories along the y-axis and their corresponding mean values with uncertainty using matplotlib −

import matplotlib.pyplot as plt
import numpy as np

# Generating data
y = np.arange(1, 6)
x_means = [5, 7, 6, 8, 9]
x_stds = [0.5, 0.3, 0.4, 0.2, 0.6]

# Plotting horizontal ribbon box plot
plt.plot(x_means, y, color='blue', label='Mean', linestyle='none', marker='o')
plt.fill_betweenx(y, np.subtract(x_means, x_stds), np.add(x_means, x_stds), color='blue', alpha=0.2) # Fixing this line

plt.xlabel('X')
plt.ylabel('Y')
plt.title('Horizontal Ribbon Box Plot')
plt.legend()
plt.grid(True)
plt.show()

执行上述代码,我们将得到以下输出 −

On executing the above code we will get the following output −

ribbon box5