简体   繁体   English

Matplotlib + Latex Rendering / twinx():次要Y轴上的字体错误?

[英]Matplotlib + Latex Rendering / twinx(): wrong font on secondary y-axis?

I am using matplotlib to generate diagrams with text being latex-rendered. 我正在使用matplotlib生成带有乳胶渲染文本的图表。 Now there's this tricky problem which I don't seem to be able to solve by myself.. 现在有一个棘手的问题,我似乎无法自己解决。

The secondary y-axis generated using twinx() shows the wrong font for ticklabels and ylabel! 使用twinx()生成的辅助y轴显示ticklabels和ylabel的字体错误! What am I doing wrong? 我究竟做错了什么? Here is what I do. 这是我的工作。

    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()

Versions in use: matplotlib 0.99.1.1 tex.. no idea ; 使用的版本:matplotlib 0.99.1.1 tex ..不知道; all on linux 全部在Linux上

ps: rendering text, title and so on works fine this way, it is only the secondary y-axis that is behaving rather badly! ps:以这种方式呈现文本,标题等效果很好,只有次要的y轴表现不佳!

The best answer to this may be to update your matplotlib version if it is at all possible. 最好的答案可能是更新您的matplotlib版本(如果可能的话)。 If subsequently you are still getting issues at least it would mean that you could open a bug report on the matplotlib github site (https://github.com/matplotlib/matplotlib/issues/new). 如果随后您仍然遇到问题,则至少意味着您可以在matplotlib github网站(https://github.com/matplotlib/matplotlib/issues/new)上打开错误报告。

I am maybe a little bit late, but no one as written a good answer yet. 我可能有点迟了,但是还没有人写出一个好的答案。 I have got the same problem and solved it by writing the ticklabels in latex: 我也遇到了同样的问题,并通过在乳胶中写上ticklabels解决了这个问题:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM