简体   繁体   English

使用matplotlib.ticker设置Matplotlib Ytick标签会产生关键错误

[英]Setting Matplotlib Ytick labels with matplotlib.ticker yields key error

I would like to set y-ticker labels, but still have them bound to the data. 我想设置y-ticker标签,但仍将它们绑定到数据。 Via this question, matplotlib: change yaxis tick labels , I am trying to do this with matplotlib.ticker . 通过这个问题, matplotlib:更改yaxis刻度标签 ,我正在尝试使用matplotlib.ticker做到这一点。

I currently just want tick marks at three positions: [.25, .5, .75] . 我目前只想在三个位置上打勾: [.25, .5, .75] The graph generates correctly, but with the print statements, I can see it iterates over each label twice and some other random values that are not in my dictionary and thus generate key errors on this line: return label_lookup[x] . 该图可以正确生成,但是使用print语句,我可以看到它遍历每个标签两次以及字典中没有的其他一些随机值,从而在此行上生成了关键错误: return label_lookup[x] The values do not appear to be the same it's run each time. 这些值似乎与每次运行的值都不相同。 What is causing this issue? 是什么导致此问题?

import matplotlib as mpl
import matplotlib.pyplot as plt

label_lookup = {.25: 'Label 1',
                .5: 'Label 2',
                .75: 'Label 3'}

def mjrFormatter(x, pos):
    print x, pos
    return label_lookup[x]

ax = plt.gca()
ax.set_yticks([.25,.5,.75], minor=False)
ax.yaxis.set_major_formatter(mpl.ticker.FuncFormatter(mjrFormatter))
plt.draw()
plt.show()

I'm not an expert at matplotlib myself, but it looks like you want to be using a FixedFormatter instead of a FuncFormatter. 我本人不是matplotlib的专家,但是您似乎想使用FixedFormatter而不是FuncFormatter。 The FixedFormatter just takes a sequence of strings instead of a function, and emits the appropriate label. FixedFormatter仅采用一系列字符串而不是函数,并发出适当的标签。 By using a FixedFormatter, you only need your 3 labels, while with a FuncFormatter, you have to have valid values for all x's passed in. 通过使用FixedFormatter,您只需要3个标签,而对于FuncFormatter,则必须具有所有传入的x的有效值。

Simply replace 只需更换

ax.yaxis.set_major_formatter(mpl.ticker.FuncFormatter(mjrFormatter))

with

ax.yaxis.set_major_formatter(mpl.ticker.FixedFormatter(['Label 1', 'Label 2', 'Label 3']))

and you'll get the same results without the KeyErrors. 而没有KeyErrors,您将获得相同的结果。 You can also then get rid of mjrFormatter , since you're no longer using it. 然后,您也可以摆脱mjrFormatter ,因为您不再使用它。

Alternatively, if you really want to use FuncFormatter, you have to be able to accept any x, not just the exact values of 0.25, 0.5, and 0.75. 另外,如果您确实想使用FuncFormatter,则必须能够接受任何x,而不仅仅是精确的0.25、0.5和0.75。 You could rewrite mjrFormatter as follows: 您可以按以下方式重写mjrFormatter

def mjrFormatter(x, pos):
    print x, pos
    if x <= .25:
         return 'Label 1'
    if x <= .5:
         return 'Label 2'
    return 'Label 3'

Your dictionary label_lookup doesn't strike me as the ideal way to do things. 您的字典label_lookup并不使我成为理想的工作方式。 You could make it work with enough effort, but the unordered nature of the dictionary makes it hard to have a simple chain of inequality checks, which is why I hardcoded the values. 您可以尽力使它工作,但是字典的无序性质使其很难拥有简单的不等式检查链,这就是为什么我对值进行硬编码的原因。

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

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