Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ploting an x-y graph with "four" axes

Generally, I'm trying to understand whether matplotlib actually has this capability.

I have a speed (on x axis) in mph vs. power (on y axis) in kW graph, to which I need to add a rotations (on second y axis, to the right) and another speed (on second x axis, up on the top) in km/h.

Power in kW is in correlation with speed in mph, while rotations is in correlation with Power, and second speed (on second x axis) is just the first speed multiplied with a convertion coefficient.

So, my question is - how can I plot a x-y plot in matplotlib with two x and two y axis?

like image 264
Rook Avatar asked Feb 04 '12 11:02

Rook


2 Answers

Looking for twinx and twiny?

import matplotlib.pyplot as plt
x = range(1,21)
plt.xlabel('1st X')
plt.ylabel('1st Y')
plt.plot(x,x,'r') # against 1st x, 1st y
plt.axis([0,50,0,25])
plt.twinx()
plt.ylabel('2nd Y')
plt.plot(x,x,'g') # against 1st x, 2nd y
plt.axis([0,50,0,20])
plt.twiny()
plt.xlabel('2nd X')
plt.plot(x,x,'b') # against 2nd x, 2nd y
plt.axis([0,10,0,20])
plt.show()

enter image description here

like image 200
Mark Tolonen Avatar answered Nov 05 '22 13:11

Mark Tolonen


My apologies, I misunderstood.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.axes import Axes

rect = 0.1, 0.1, 0.8, 0.8

fig = plt.figure()
ax1 = fig.add_axes(rect)

t = np.arange(0.01, 10.0, 0.01)

ax1.plot(t, np.exp(t), 'b-') # Put your speed/power plot here
ax1.set_xlabel('Speed (mph)', color='b')
ax1.set_ylabel('Power', color='b')

ax2 = fig.add_axes(rect, frameon=False)
ax2.yaxis.tick_right()
ax2.yaxis.set_label_position('right')
ax2.xaxis.tick_top()
ax2.xaxis.set_label_position('top')

ax2.plot(t, np.sin(2*np.pi*t), 'r-') # Put your speed/rotation plot here
ax2.set_xlabel('Speed (kmph)', color='r')
ax2.set_ylabel('Rotations', color='r')

plt.show()

enter image description here

like image 4
Alex L Avatar answered Nov 05 '22 13:11

Alex L