我有兩個資料系列,都有正值,我想將它們顯示在同一個圖表中,如下圖所示。如果我反轉第二個系列,我會得到想要的結果,但是這樣 Y 軸值將是負數。有沒有基本在 Y 軸上顯示絕對值的解決方案?
提前致謝!

uj5u.com熱心網友回復:
您可以設定格式化程式以顯示絕對值:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(100)
y1 = np.random.normal(0.1, 1, 100).cumsum()
y1 -= y1.min()
y2 = np.random.normal(0.1, 1, 100).cumsum()
y2 -= y2.min()
fig, ax = plt.subplots()
ax.bar(x, y1)
ax.bar(x, -y2)
ax.yaxis.set_major_formatter(lambda x, pos: f'{abs(x):g}')
ax.margins(x=0)
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/418144.html
標籤:
