在Matplotlib和OSX中使用数学模式和下标时出现错误的偏移量 [英] Wrong offset when using math mode and subscripts in Matplotlib and OSX

查看:169
本文介绍了在Matplotlib和OSX中使用数学模式和下标时出现错误的偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些图,我想在某些标签中使用子索引,但是每当我使用数学模式时,所有标签都会出现偏移.我可以为所有标签设置一些偏移量吗?使用数学模式有什么我想念的吗?

I have some plots where I would like to use subindices in some labels but whenever I use math mode all the labels appear shifted. Can I set some offset for all my labels? Is there anything I'm missing for using math mode?

这是标签中没有数学模式的图:

This is the plot without math mode in the labels:

这是数学模式下的样子(注意刻度):

And this is what it looks like with math mode (notice the ticks):

作为参考,这是我的完整代码(我从那里得到了堆叠的代码):

For reference, here is my full code (I got the stacked code from ):

import numpy as NP
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import matplotlib.cm as cm
data = '''0    0    0    0    0    0    0    0
0    0    0    0    0    0    0.015    0.015
0    0    0    0    0    0    0    0
0    0    0    0    0.04    0.04    0    0
0    0    0    0    0.03    0.01    0.19    0.14
0    0    0.772    0    0.07    0.01    0.12    0.11
0    0.879    0    0    0    0.07    0    0.085
0.056    0    0    0    0    0    0    0
'''.splitlines()
data = tuple(reversed([NP.array([float(j) for j in i.split('    ')]) for i in data]))
colors = cm.rainbow(NP.linspace(0, 1, 8))
axes = plt.figure().add_subplot(111)
axes.set_xticklabels([r'$m_%d$'%i for i in ([i+1 for i in range(8)])])
plt.stackplot(NP.arange(8)+1,
          data, 
          colors=colors)
plt.xlim(1,8)
plt.ylabel("Error")  
plt.legend([mpatches.Patch(color=i) for i in colors], 
           [r'$m_%d$'%i for i in ([i+1 for i in range(8)])])
plt.show()

更新:问题出在用于交互式显示的后端上

按照我尝试写入文件的注释中提供的提示,标签会正确显示.问题似乎出在MacOSX后端.

Following the hints provided in the comments I tried writing to a file and the labels appear properly. The problem seem to be on the MacOSX backend.

  • Python 2.7.9(默认值,2014年12月11日,02:36:08)[darwin上的[GCC 4.2.1兼容Apple LLVM 5.1(clang-503.0.40)]
  • matplotlib.版本 1.4.3
  • matplotlib.get_backend()MacOSX
  • Python 2.7.9 (default, Dec 11 2014, 02:36:08) [GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
  • matplotlib.version 1.4.3
  • matplotlib.get_backend() MacOSX

推荐答案

在MacOSX后端上似乎有一个Matplotlib错误.为了解决这个问题,我不得不切换后端.我从FAQ中尝试了几种( http://matplotlib.org/faq /usage_faq.html#what-is-a-backend ),使用WXAgg可获得最佳效果. TkAgg非常缓慢,WX不支持数学模式. 如果有人感兴趣,导入pyplot之前要添加的代码是:

There seems to be a bug on the MacOSX backend for Matplotlib. In order to solve it I had to switch the backend. I tried several from the FAQ (http://matplotlib.org/faq/usage_faq.html#what-is-a-backend) and I got best results using WXAgg. TkAgg was very sluggish and WX does NOT support math mode. If anyone is interested, the code to be added before importing pyplot is:

import matplotlib
matplotlib.use('WXAgg')

这是结果(所有看起来都略有不同):

This are the results (all look slightly different):

WXAgg

TkAgg

WX

这篇关于在Matplotlib和OSX中使用数学模式和下标时出现错误的偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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