【问题标题】:Matplotlib + Latex Rendering / twinx(): wrong font on secondary y-axis?Matplotlib + Latex Rendering / twinx():辅助y轴上的字体错误?
【发布时间】:2012-07-12 18:22:02
【问题描述】:

我正在使用 matplotlib 生成带有乳胶渲染文本的图表。现在有一个棘手的问题,我似乎无法自己解决..

使用 twinx() 生成的辅助 y 轴显示错误的刻度标签和 ylabel 字体!我究竟做错了什么? 这就是我所做的。

    from matplotlib import pyplot as plt
    from matplotlib import rc
    from matplotlib.figure import Figure               
    from matplotlib.axes import Axes    
    from matplotlib.lines import Line2D

    rc('font',**{'family':'serif','sans-serif':['Computer Modern Roman']})
    rc('text', usetex=True)

    fig = plt.figure(figsize = (4,4) )                                
    ax = Axes(fig, [.1,.1,.8,.8])  
    ax_ = ax.twinx()                            
    fig.add_axes(ax)

    fig.add_axes(ax_)       

    l = Line2D([0, 1],[0, 1], color='r')

    ax.set_ylabel(r'Label')
    ax_.set_ylabel(r'Label')

    ax.add_line( l )

    plt.show()

正在使用的版本: matplotlib 0.99.1.1 德州..不知道 ;全部在linux上

ps: 以这种方式渲染文本、标题等效果很好,只是次要 y 轴表现得相当糟糕!

【问题讨论】:

  • 副轴上的字体是'stronger',我不知道正确的排版表达方式。
  • 我无法重现您的问题。我正在使用 matplotlib 1.1。您可以尝试将图形保存为 pdf 并检查它是否仍然存在。
  • @nina 你能上传你看到的图片吗?我也无法重现 1.1.1rc 的问题
  • 如下所述,我将尝试通过更新我的 matplotlib 来解决这个问题。本来可以上传图片的,但对我来说还不可能,首先需要更多的声誉。我会尽快报告发生的事情!

标签: python matplotlib


【解决方案1】:

如果可能的话,最好的答案可能是更新你的 matplotlib 版本。如果随后您仍然遇到问题,至少这意味着您可以在 matplotlib github 站点 (https://github.com/matplotlib/matplotlib/issues/new) 上打开错误报告。

【讨论】:

  • 好的,我想我明天会找时间做更新,无论如何,谢谢!我希望这能解决问题!
【解决方案2】:

我可能有点晚了,但还没有人写出一个好的答案。我遇到了同样的问题并通过在乳胶中编写刻度标签来解决它:

import matplotlib.pyplot as plt
labels = [r"$1.$", r"$1.5$", r"$2.$"]
ticks = [1., 1.5, 2.]
plt.set_xticks(ticks)
plt.set_xticklabels(labels)

【讨论】:

    猜你喜欢
    • 2013-01-23
    • 1970-01-01
    • 2020-11-09
    • 2019-02-07
    • 1970-01-01
    • 2022-06-28
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    相关资源
    最近更新 更多