Attila Fenyvesi
Attila Fenyvesi

Reputation: 105

PyPlot - Positive values on Y-axis in both directions

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!

enter image description here

Upvotes: 2

Views: 952

Answers (1)

JohanC
JohanC

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()

showing negative ticks as positive

Upvotes: 4

Related Questions