如何避免重叠误差条并替换 X 和 Y 轴?

时间:2021-05-08 13:02:48

标签: python plot overlapping yaxis x-axis

我是 matplotlib 的新手,我请求您帮助解决我的小问题。我正在分享下面的图表,以下是问题:

1- 我想替换 x 轴和 y 轴 2- 对我来说最重要的是误差条应该是水平的(在下面的图表中是垂直的)。

图中的一些错误栏是重叠的,我尝试使用转换命令来避免这个问题。正如我之前所说,如果我能管理 X 和 Y 轴的替换,我会很高兴。

下面分享我写的代码:

import ax as ax
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.transforms import Affine2D

y_values = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
p1 = [1, 0.77, 0.67, 0.85, 0.78, 1.05, 0.63]
p2 = [3, 2, 1.5, 1.20, 1.10, 1.40, 1.10]

x_err = [0.1, 0.2, 0.4, 0.5, 0.3, 0.2, 0.3]
y_err = [0.6, 0.2, 0.4, 0.5, 0.3, 0.2, 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(y_values, p1, x_err, marker="o", linestyle="none", transform=trans1)
er2 = ax.errorbar(y_values, p2, y_err, marker="o", linestyle="none", transform=trans2)

errorbar plot

0 个答案:

没有答案