Skip to content

Instantly share code, notes, and snippets.

@thomasaarholt
Created December 10, 2019 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasaarholt/702a352927079c372111a53bbe7cdcac to your computer and use it in GitHub Desktop.
Save thomasaarholt/702a352927079c372111a53bbe7cdcac to your computer and use it in GitHub Desktop.
Matplotlib plot with 1/T and second units
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twiny()
X = np.linspace(1,1000,1000)
Y = np.cos(X/200)
ax1.plot(X,Y)
ax1.set_xlabel(r"Original x-axis: $X$")
new_tick_locations = np.array([300, 200, 100])
def tick_function(X):
V = 1/X
return ["%.3f" % z for z in V]
ax2.set_xlim(ax1.get_xlim())
ax2.set_xticks(new_tick_locations)
ax2.set_xticklabels(tick_function(new_tick_locations))
ax2.set_xlabel(r"Modified x-axis: $1/(1+X)$")
new_tick_locations = np.array([1, 0.5, -0.75])
ax3 = ax1.twinx()
ax3.set_ylim(ax1.get_ylim())
ax3.set_yticks(new_tick_locations)
ax3.set_yticklabels(tick_function(new_tick_locations))
ax3.set_ylabel(r"Modified x-axis: $1/(1+X)$")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment