python - how to get matplotlib physical (not data) scale -
i have simple code:
import numpy np import matplotlib.pyplot plt matplotlib.backends.backend_pdf import pdfpages matplotlib.patches import ellipse plotfilename="test.pdf" pdf = pdfpages(plotfilename) fig=plt.figure(1) ax1=fig.add_subplot(111) plt.xlim([0,10]) plt.ylim([0,10]) ax1.plot([0,10],[0,10]) e=0.0 theta=0 maj_ax=2 min_ax=maj_ax*np.sqrt(1-e**2) const=1 ax1.add_artist(ellipse((5, 5), maj_ax, const*min_ax, angle=theta, facecolor="green", edgecolor="black",zorder=2, alpha=0.5)) plt.grid() pdf.savefig(fig) pdf.close() plt.close()
here how looks:
as see code, should circle, isn't! have narrowed problem down const
term in line 16. don't want use ax1.axis("equal")
because data don't have same scales on vertical , horizontal. 1 tell me how can ask matplotlib tell me aspect ratio using can set const
term correctly have circle in end?
in other words want know ratio of horizontal vertical axis "physical" length (for example, printed out).
i appreciate suggestions, in advance
one option explicitly define figure size... may need specify subplot parameters if using non-default settings. adjust figsize , subplot parameters needed non-equal horizontal , vertical scales. example:
import numpy np import matplotlib.pyplot plt matplotlib.patches import ellipse fig = plt.figure(figsize=(6,4)) fig.subplots_adjust(left=0.1, right=0.9, bottom=0.1, top=0.9) ax1 = fig.add_subplot(111, xlim=(-2.5,12.5), ylim=(0,10)) ax1.plot((0,10), (0,10)) maj_ax, e, theta = 2, 0, 0 min_ax = maj_ax * np.sqrt(1 - e**2) ax1.add_artist(ellipse((5, 5), maj_ax, min_ax, angle=theta, fc="green", ec="black", zorder=2, alpha=0.5)) plt.grid() plt.show()
Comments
Post a Comment