python - Plotting a second scaled y axis in matplotlib from one set of data -
i have 2 views of same data, calls need have y-axis scaled appropriately first natural y-axis. when plot {x,y}
data, left y-axis shows y, right y-axis shows 1/y or other function. not ever want plot {x, f(x)}
or {x, 1/y}
.
now complicate matters using .plt
style of interaction rather axis method.
plt.scatter(x, y, c=colours[count], alpha=1.0, label=chart, lw = 0) plt.ylabel(y_lbl) plt.xlabel(x_lbl)
is there way - plt? or case of generating 2 overlain plots , changing alpha appropriately?
i had check previous (duplicate) question , comments understand want. secondary y-axis can still use twinx
. can use set_ylim
make sure has same limits first. put tick labels according function (in case 1/y
) can use custom funcformatter.
import matplotlib.pyplot plt import numpy np import matplotlib.ticker mticker fig, ax1 = plt.subplots(1,1) ax1.set_xlabel('x') ax1.set_ylabel('y') # plot x = np.linspace(0.01, 10*np.pi, 1000) y = np.sin(x)/x ax1.plot(x, y) # add twin axes , set limits matches first ax2 = ax1.twinx() ax2.set_ylabel('1/y') ax2.set_ylim(ax1.get_ylim()) # apply function formatter formatter = mticker.funcformatter(lambda x, pos: '{:.3f}'.format(1./x)) ax2.yaxis.set_major_formatter(formatter) plt.show()
result:
Comments
Post a Comment