python输出数据坐标对数_python-在Matplotlib中以对数刻度将文本旋转到...

问题

我正在使用对数标度的数据,并希望将其旋转以适合一条线.我知道模型,但不确定要插入哪个角度才能恢复正确的旋转.经过一番尝试和错误后,我知道答案是我要求的轴限制在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]时,我为锚点添加了垂直偏移.如果解决方案需要考虑到这一点,那就更好了.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值