Created
October 16, 2017 10:17
-
-
Save dneuman/d9311248ef8e03e8c8c238bc7a778a22 to your computer and use it in GitHub Desktop.
Utility function to use with matplotlib.pyplot to put a mirrored y-axis on the right of the plot. Uses state of pyplot to determine appropriate axes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
import matplotlib.ticker as tk | |
def Mirror(prec=1): | |
"""Adds y-axis that's a mirror of the y-axis on left. | |
Parameters | |
---------- | |
prec : int (opt) Default=1 | |
Number of significant digits (precision) to use on scale | |
Notes | |
----- | |
Use this function just before the final fig.show() command. | |
""" | |
ax = plt.gca() | |
ax2 = ax.twinx() | |
fmt = '% .{}f'.format(prec) # adds a space if number is >0 | |
ax.yaxis.set_major_formatter(tk.FormatStrFormatter(fmt)) | |
ax2.yaxis.set_major_formatter(tk.FormatStrFormatter(fmt)) | |
ax2.grid(False) # is sitting on top of lines | |
ax2.set_yticks(ax.get_yticks()) | |
ax2.set_ylim(ax.get_ylim()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment