使用Matplotlib在半对数图中的长宽比 [英] Aspect ratio in semi-log plot with Matplotlib

查看:142
本文介绍了使用Matplotlib在半对数图中的长宽比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在matplotlib中绘制函数时,该图由矩形框起来.我希望此矩形的长度和高度的比率由黄金平均值给出,即dx/dy = 1.618033 ...

When I plot a function in matplotlib, the plot is framed by a rectangle. I want the ratio of the length and height of this rectangle to be given by the golden mean ,i.e., dx/dy=1.618033...

如果x和y比例是线性的,我使用google找到了这个解决方案

If the x and y scale are linear I found this solution using google

import numpy as np
import matplotlib.pyplot as pl
golden_mean = (np.sqrt(5)-1.0)/2.0
dy=pl.gca().get_ylim()[1]-pl.gca().get_ylim()[0]
dx=pl.gca().get_xlim()[1]-pl.gca().get_xlim()[0]
pl.gca().set_aspect((dx/dy)*golden_mean,adjustable='box')

如果是对数-对数图,我想出了这个解决方案

If it is a log-log plot I came up with this solution

dy=np.abs(np.log10(pl.gca().get_ylim()[1])-np.log10(pl.gca().get_ylim()[0]))
dx=np.abs(np.log10(pl.gca().get_xlim()[1])-np.log10(pl.gca().get_xlim()[0]))
pl.gca().set_aspect((dx/dy)*golden_mean,adjustable='box')

但是,对于半对数图,当我调用set_aspect时,会得到

However, for a semi-log plot, when I call set_aspect, I get

UserWarning: aspect is not supported for Axes with xscale=log, yscale=linear

有人可以考虑解决此问题的方法吗?

Can anyone think of a work-around for this?

推荐答案

最简单的解决方案是记录数据,然后对lin-lin使用该方法.

the most simple solution would be to log your data and then use the method for lin-lin.

然后您可以标记轴以使其看起来像普通的对数图.

you can then label the axes to let it look like a normal log-plot.

ticks = np.arange(min_logx, max_logx, 1)
ticklabels = [r"$10^{}$".format(tick) for tick in ticks]

pl.yticks(ticks, ticklabels)

如果值大于10e9,则需要三对大括号,两对用于LaTeX大括号,一对用于.format()

if you have higher values than 10e9 you will need three pairs of braces, two pairs for the LaTeX braces and one for the .format()

ticklabels = [r"$10^{{{}}}$".format(tick) for tick in ticks]

如果您还希望0.1ex ... 0.9ex的价格变动,您也要使用次要价格变动:它们必须位于log10(1),log10(2),log10(3)...,log10(10),log10(20)...

if you want also the ticks for 0.1ex ... 0.9ex, you want to use the minor ticks as well: they need to be located at log10(1), log10(2), log10(3) ..., log10(10), log10(20) ...

您可以使用以下方法创建和设置它们:

you can create and set them with:

minor_ticks = []
for i in range(min_exponent, max_exponent):
    for j in range(2,10):
         minor_ticks.append(i+np.log10(j))


plt.gca().set_yticks(minor_labels, minor=True)

这篇关于使用Matplotlib在半对数图中的长宽比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆