设置图的宽度并从子图的长宽比推断高度 [英] Set width of plot and infer height from aspect ratio of subplots

查看:62
本文介绍了设置图的宽度并从子图的长宽比推断高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常必须创建具有固定宽度(例如,文章的列宽)并且其子图在数据坐标系中具有固定比率的图.我想手动设置宽度,并获得具有所选宽度,子图的长宽比和适当高度的图形.

I often have to create plots which have a fixed width (e.g. the column width of an article) and whose subplots have a fixed ratio in the data coordinate system. I want to set the width manually and obtain a plot with this chosen width, aspect ratio of the subplots and the appropriate height.

示例:文章的各列宽度为8.5厘米.我想创建一个带有两个子图(轴)的图,并为两个轴选择 ax.set_aspect("equal") 选项,以便 y 轴上的 1 个单位在 x 轴上占据与一个单元相同的物理空间.我的情节现在应该有一个 8.5 厘米的总宽度,但高度应该从 matplotlib 计算,考虑到子图的纵横比以及 xlimylim价值.

Example: The columns of an article have a width of 8.5cm. I want to create a plot with two subplots (axes) and for both axes the option ax.set_aspect("equal") is chosen so that 1 unit on the y axis takes the same physical space as one unit on the x axis. My plot should now have a total width of 8.5cm but the height should be calculated from matplotlib by taking into account the aspect ratio of the subplots as well as the xlim and ylim values.

我怎样才能做到这一点?

How can I achieve this?

此外,如何确保绘图中使用的字体大小与文章中使用的字体大小相同?因此,如果我的文章在 11pt 中设置了时间",我如何在我的情节中获得相同大小的相同字体?我尝试使用

Additionally, how do I make sure that the font size used in the plots is the same as in the article? So if my article is set with "times" in 11pt, how do I get the same font with the same size in my plot? I tried to use

params = {'text.usetex': True,
          'font.size': 11,
          'font.family': 'times',
          'text.latex.unicode': True,
          }
plt.rcParams.update(params)

但是例如,我的绘图中的x和y字体现在比文章中大得多.我认为 'font.size' 会更新所有字体大小?

but for example the x- and y-label fonts in my plot are now much larger than in the article. I thought that 'font.size' updates all font sizes?

推荐答案

在给定width和subplot参数的情况下,您可以计算图形的高度.下面假设子图的宽度相等,并且只有一行子图并且它们都有一个定义的方面(即不是自动").

You can calculate the height of the figure given the width and the subplot parameters. The following assumes that the subplots are equal in width and that there is only one row of subplots and that they all have a defined aspect (i.e. not "auto").

import numpy as np
import matplotlib.pyplot as plt

fig, (ax1,ax2) = plt.subplots(1,2, figsize=(8.5,8.5))
for ax in (ax1, ax2):
    ax.set_aspect("equal")

ax1.plot([1,3], [4,6], marker="o")
ax2.plot([26,84], [64,-13], marker="s")

def resize(axes):
    # this assumes a fixed aspect being set for the axes.
    for ax in axes:
        if ax.get_aspect() == "auto":
            return
        elif ax.get_aspect() == "equal":
            ax.set_aspect(1)
    fig = axes[0].figure
    s = fig.subplotpars
    n = len(axes)
    axw = fig.get_size_inches()[0]*(s.right-s.left)/(n+(n-1)*s.wspace)
    r = lambda ax: np.diff(ax.get_ylim())[0]/np.diff(ax.get_xlim())[0]*ax.get_aspect()
    a = max([r(ax) for ax in axes])
    figh = a*axw/(s.top-s.bottom)
    fig.set_size_inches(fig.get_size_inches()[0],figh)

resize((ax1,ax2))

plt.show()

这篇关于设置图的宽度并从子图的长宽比推断高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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