DaniV
DaniV

Reputation: 489

How to invert axes of a function with matplotlib

I have the following function, what I am looking for is to invert the x and y axes.

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

major_ticks = np.arange(-2, 10+2, 1)
minor_ticks = np.arange(-2, 10+2, 0.25)

ax.set_xticks(major_ticks)
ax.set_xticks(minor_ticks, minor=True)
ax.set_yticks(major_ticks)
ax.set_yticks(minor_ticks, minor=True)

ax.grid(which='both')

ax.grid(which='minor', alpha=0.2)
ax.grid(which='major', alpha=0.5)

plt.title('Image')
plt.xlabel('Height (m)')
plt.ylabel('Length (m)')
plt.axis('equal')

function = np.array([0, 0.52, 0.92, 0.8])    
plt.plot(function)

The following graph illustrates how it should look, best regards. enter image description here

Upvotes: 1

Views: 64

Answers (1)

tom10
tom10

Reputation: 69242

The plot command lets you specify both x and y but when you call it with a single vector, it assumes the first vector is a list of ints. So to invert the plot, create the default x and then swap x and y.

enter image description here

plt.plot(function, np.arange(4), '.-') # function is now treated as x, and I've created the default x and am using it for y.

Also, I renamed the axes and removed most of the formatting so this looked right, but the only interesting change is the plot command.

Upvotes: 2

Related Questions