如何在 matplotlib 中避免重叠误差棒?
为了避免 matplotlib 中重叠的误差棒,我们可以采取以下步骤——
步骤
设置图形大小并调整子图间和周围的边距。
创建名称列表。
获得 y1 和 y2、误差 ye1、ye2 的数据点。
创建一个图形和一组子图。
创建一个可变 2D 仿射变换 trans1 和 trans2。
以带有附加误差棒的线和/或标记形式绘制 y 针对 x。
要显示图形,请使用 show() 方法。
示例
import numpy as np import matplotlib.pyplot as plt from matplotlib.transforms import Affine2D plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = ['Jack', 'James', 'Tom', 'Garry'] y1, y2 = np.random.randn(2, len(x)) ye1, ye2 = np.random.rand(2, len(x))*4+0.3 fig, ax = plt.subplots() trans1 = Affine2D().translate(-0.1, 0.0) + ax.transData trans2 = Affine2D().translate(0.1, 0.0) + ax.transData er1 = ax.errorbar(x, y1, yerr=ye1, marker="*", linestyle="none", transform=trans1) er2 = ax.errorbar(x, y2, yerr=ye2, marker="o", linestyle="none", transform=trans2) plt.show()
输出
它将生成以下输出——

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言
C++
C#
MongoDB
MySQL
Javascript
PHP