使用对数刻度 x 轴,您无法为条形设置恒定宽度。例如。第一个条将介于0 和0.000002 之间(0 在对数刻度上处于负无穷大)。
您可以将 x 位置用于条形的左边缘,将下一个 x 位置用于右边缘:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
x = [0.000001, 0.00001, 0.0001, 0.001, 0.01, 0.1, 1.0]
height = [5.3, 1.8, 8.24, 5.8, 2.8, 3.3, 4.2]
plt.xscale("log")
widths = np.diff(x + [x[-1] * 10])
plt.bar(x, height, widths, align='edge', facecolor='dodgerblue', edgecolor='white', lw=2)
plt.show()
如果要将条形“居中”在原始 x 值周围,则需要计算每个条形在对数空间中的开始和结束位置。获得更多条间距的最简单方法是设置更厚的白色边框。
import matplotlib.pyplot as plt
import numpy as np
x = [0.000001, 0.00001, 0.0001, 0.001, 0.01, 0.1, 1.0]
height = [5.3, 1.8, 8.24, 5.8, 2.8, 3.3, 4.2]
plt.xscale("log")
padded_x = [x[0] / 10] + x + [x[-1] * 10]
centers = [np.sqrt(x0 * x1) for x0, x1 in zip(padded_x[:-1], padded_x[1:])]
widths = np.diff(centers)
plt.bar(centers[:-1], height, widths, align='edge', facecolor='dodgerblue', edgecolor='white', lw=4)
plt.margins(x=0.01)
plt.show()
如果您计算每个条的新左右位置,您还可以有一个可配置的宽度:
import matplotlib.pyplot as plt
import numpy as np
x = [0.000001, 0.00001, 0.0001, 0.001, 0.01, 0.1, 1.0]
height = [5.3, 1.8, 8.24, 5.8, 2.8, 3.3, 4.2]
plt.xscale("log")
padded_x = [x[0] / 10] + x + [x[-1] * 10]
width = 0.3 # 1 for full width, closer to 0 for thinner bars
lefts = [x1 ** (1 - width / 2) * x0 ** (width / 2) for x0, x1 in zip(padded_x[:-2], padded_x[1:-1])]
rights = [x0 ** (1 - width / 2) * x1 ** (width / 2) for x0, x1 in zip(padded_x[1:-1], padded_x[2:])]
widths = [r - l for l, r in zip(lefts, rights)]
plt.bar(lefts, height, widths, align='edge', facecolor='dodgerblue', lw=0)
plt.show()