Reputation: 105
I have two data series, both have positive values, and I'd like to display them in the same graph as shown in the image below. If I invert the second series I get the desired result, but this way the Y-axis values will be negative. Is there a solution to basically display absolute values on the Y-axis?
Thanks in advance!
Upvotes: 2
Views: 952
Reputation: 80509
You can set the formatter to show the absolute value:
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()
Upvotes: 4