Matplotlib代号 [英] Matplotlib Ticker

查看:129
本文介绍了Matplotlib代号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以给我一个如何使用以下tickFormatters的示例. 文档对我无益.

Can someone give me an example of how to use the following tickFormatters. The docs are uninformative to me.

ticker.StrMethodFormatter() ticker.IndexFormatter()

ticker.StrMethodFormatter() ticker.IndexFormatter()

例如,我可能会认为

x = np.array([ 316566.962,  294789.545,  490032.382,  681004.044,  753757.024,
            385283.153,  651498.538,  937628.225,  199561.358,  601465.455])
y = np.array([ 208.075,  262.099,  550.066,  633.525,  612.804,  884.785,
            862.219,  349.805,  279.964,  500.612])
money_formatter = tkr.StrMethodFormatter('${:,}')

plt.scatter(x,y)
ax = plt.gca()
fmtr = ticker.StrMethodFormatter('${:,}')
ax.xaxis.set_major_formatter(fmtr)

将我的刻度标签设置为美元符号,并在数千个地方加逗号分隔

would set my tick labels to be dollar signed and comma sep for thousands places ala

['$300,000', '$400,000', '$500,000', '$600,000', '$700,000', '$800,000', '$900,000']

但是我得到一个索引错误.

but instead I get an index error.

IndexError: tuple index out of range

对于IndexFormatter文档说:

从标签列表中设置字符串

Set the strings from a list of labels

我真的不知道这意味着什么,当我尝试使用它时,我的抽搐消失了.

don't really know what this means and when I try to use it my tics disappear.

推荐答案

StrMethodFormatter 确实通过提供可以使用format方法设置格式的字符串来工作.因此,使用'${:,}'的方法朝着正确的方向发展.

The StrMethodFormatter works indeed by supplying a string that can be formatted using the format method. So the approach of using '${:,}' goes in the right direction.

但是从我们学习的文档

用于该值的字段必须标记为x,并且用于该位置的字段必须标记为pos.

The field used for the value must be labeled x and the field used for the position must be labeled pos.

这意味着您需要为该字段提供实际的标签x.另外,您可能希望将数字格式指定为g而不使用小数点.

This means that you need to give an actual label x to the field. Additionally you may want to specify the number format as g not to have the decimal point.

fmtr = matplotlib.ticker.StrMethodFormatter('${x:,g}')

IndexFormatter 在这里没什么用.如您所知,您将需要提供标签列表.这些标签用于索引,从0开始.因此,使用此格式化程序将需要使x轴从零开始并在一些整数范围内.

The IndexFormatter is of little use here. As you found out, you would need to provide a list of labels. Those labels are used for the index, starting at 0. So using this formatter would require to have the x axis start at zero and ranging over some whole numbers.

示例:

plt.scatter(range(len(y)),y)
fmtr = matplotlib.ticker.IndexFormatter(list("ABCDEFGHIJ"))
ax.xaxis.set_major_formatter(fmtr)

在此处,刻度线位于(0,2,4,6,....)处,并且列表(A, C, E, G, I, ...)中的各个字母用作标签.

Here, the ticks are placed at (0,2,4,6,....) and the respective letters from the list (A, C, E, G, I, ...) are used as labels.

这篇关于Matplotlib代号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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