Figure avec deux axes des abscisses (Matplotlib) ?


Ajouter un second axes des abscisses sous matplotlib (source)

Figure avec deux axes des abscisses (Matplotlib)

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twiny()

X = np.linspace(0,1,1000)
Y = np.cos(X*10)

ax1.plot(X,Y)
ax1.set_xlabel(r"Original x-axis: $X$")

new_tick_locations = np.array([.2, .5, .9])

def tick_function(X):
    V = 1/(1+X)
    return ["%.3f" % z for z in V]

ax2.set_xticks(new_tick_locations)
ax2.set_xticklabels(tick_function(new_tick_locations))
ax2.set_xlabel(r"Modified x-axis: $1/(1+X)$")

plt.ylim(-1.0,1.0)

plt.savefig('ShareAxes_03.png')    
plt.show()

Recherches associées

Image

of