问题
我正在使用对数标度的数据,并希望将其旋转以适合一条线.我知道模型,但不确定要插入哪个角度才能恢复正确的旋转.经过一番尝试和错误后,我知道答案是我要求的轴限制在10度左右.
微机
import matplotlib.pylab as plt
import numpy as np
plt.clf()
plt.yscale('log')
plt.ylim((1e-11, 1e-1)) # Other data is usually plotted and these are the ranges I need.
plt.xlim((-0.2, 7.2))
x_fit = np.linspace(0.8, 3.2, 1000)
y_ols = (lambda x: np.exp(np.log(2)*(-20.8 + -1.23 * x)))(x_fit) # I get these numbers from OLS fitting.
plt.plot(x_fit, y_ols, 'b-', dashes='', label='__nolegend__')
plt.gca().text(np.min(x_fit), 1.2*y_ols[0], r'$O(2^{{ {:.3}x }})$'.format(-1.23), rotation=-10).set_bbox(dict(facecolor='w', alpha=0.7, edgecolor='k', linewidth=0)) # There are several others lines which have been omitted.
Remarks on the plot to answer comments
In my full plot I use a dual axis (both on log scales) with the twinx() feature. All the data are plotted on ax1 which uses a log-10 scale (as shown). (I could be more explicit and write yscale('log', basey=10)…). Ultimately I want a base-10 axis.
The model used in making y_ols comes from a regression fit to some original data and requires base-2. On a log scale it is easy enough to recover the gradient in any required base.
使用渐变
通过使用np.arctan和np.gradient和角度(以弧度为单位)的混合,以对数刻度恢复梯度很容易,但是我似乎无法恢复接近10度(0.17弧度)的数字).
transData.transform_angles(np.array((np.mean(np.gradient(np.log10(y_ols), np.mean(np.diff(x_fit)))),)), np.array([np.min(x_fit), 1.2*y_ols[0]]).reshape((1, 2)), radians=True)[0]
给出-1.6弧度(大约-90度),而我需要一个更接近0.17弧度的数字.也许我应该使用其他基准,或者我做错了所有的事(因此而发帖).
附加功能-垂直偏移
从代码中可以看出,当使用1.2 * y_ols [0]时,我为锚点添加了垂直偏移.如果解决方案需要考虑到这一点,那就更好了.