【问题标题】:Figsize and axes linewidth suddenly smaller after making text italicFigsize和axes linewidth在使文本斜体后突然变小
【发布时间】:2022-02-03 06:50:59
【问题描述】:

我刚刚改了这个

    import matplotlib.pyplot as plt
    import numpy as np
    import numpy.ma as ma

    # ... more code here...
 
    f = plt.figure(figsize=(12,8)) #width height
    
    axarr = plt.axes([0.115,0.1,0.76,0.7]) #[left, bottom, width, height]
    axarr2=axarr.twinx()
    axarr3=axarr.twinx()

    # ... more code here...

    if TheEarlySpatialSlope2 < 0 and TheEarlySpatialSlope3 < 0:
        sentenceEarly = 'q\u2193, T\u2193'
    elif TheEarlySpatialSlope2 < 0 and TheEarlySpatialSlope3 > 0:
        sentenceEarly = 'q\u2193, T\u2191'

斜体:

    if TheEarlySpatialSlope2 < 0 and TheEarlySpatialSlope3 < 0:
        sentenceEarly = '$\it{q}$\u2193, $\it{T}$\u2193'
    elif TheEarlySpatialSlope2 < 0 and TheEarlySpatialSlope3 > 0:
        sentenceEarly = '$\it{q}$\u2193, $\it{T}$\u2191

然后我又改回来了。在进行任何更改之前,绘制了左侧的图像(这是我想要的)。当使用斜体字母时,我得到了右侧的图像(细线,奇怪的图形大小),并且在将内容更改回第一个版本之后。我以前经历过这种情况,只有在 MacOs 中重置系统管理控制器 (SMC) 和 NVRAM / PRAM 才有帮助。现在,重置并没有让我恢复原状。

我不能给你一个小例子,因为我的代码太长了,很明显,这与代码本身无关,因为我没有更改任何内容的 python 文件的副本现在正在生成奇怪的图像出色地。看来我的电脑更改了设置或其他东西。显然,其他用户也有这个问题:

[matplotlib figures scale weirdly / can't get a nice small figure][2]
[Attempt to create a dynamically changing plot in console and notebook][3]
[figure changes size at end of loop if drawn dynamically][4]
[Figure doesn't fill canvas in interactive plot with Matplotlib in Jupyter notebook][5]

我只是python的初学者,不了解深入的hack,所以请问您是否可以指出我的问题的解决方案。

【问题讨论】:

  • 是的,我见过这种神器弹出。我看到你说你不能举一个小例子,但是你的代码有多便携?与其刷新系统,不如在远程“新”机器上运行代码?我有两个 repos herehere,您可以通过按 launch binder 访问并获得最新的绘图电缆会话。两者都默认在“经典”笔记本界面中打开 - 单击左上角的 Jupyter 徽标以切换到 JupyterLab。
  • Continued.... JupyterLab 界面允许从本地系统轻松拖放到左侧面板上的文件导航器中。您可以将笔记本和数据放在那里(希望如此)并运行(希望如此)。如果您需要安装任何不存在的东西,请在笔记本的单元格中使用 %pip install%conda install下载任何有用的内容,因为它们是临时会话,十分钟不活动后会超时。
  • 12x8 是一个很大的数字,假设您使用 12 pt 字体,您的第二个图看起来大小合适。当您不谈论您的环境或您如何尝试显示数字时,就不可能说出为什么您的数字会改变大小。
  • 谢谢@Wayne!我的代码还不是很便携,但我会尝试。 @Jody Klymak,感谢您的提示!我已经删除了“figsize=(12,8)”,并得到了我漂亮的情节,用斜体字母!但是,现在图表顶部有一个巨大的空白区域,我想裁剪我的图形以最小化白色边框。除了 plt.figure(figsize=(12,8)) 还有其他方法吗?

标签: python matplotlib


【解决方案1】:

我换了

f = plt.figure(figsize=(12,8))

f = plt.figure()

并添加

bbox_inches = 'tight'

保存图像以对其进行裁剪时:

plt.savefig(TheFile+".eps", bbox_inches = 'tight')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多