Matplotlib:自动修改轴标签 [英] Matplotlib: automatically modify axis labels

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

问题描述

我知道可以通过手动设置来更改轴标签.(例如:修改刻度标签文本)

I'm aware that it's possible to change axis labels by setting them manually. (E.g.: Modify tick label text)

但是,这显然只有在您知道自己想要什么标签时才有效,而我的情况并非如此.

However, this obviously only works if you know what labels you want, which is not the case for me.

这是我要完成的工作的一个示例:我有两个numpy数组: x 包含1到366之间的数字(但不一定是全部),代表2016年的日子."y"包含其他数字.我想作一个散点图,分别是"y"和"x":

Here's an example of what I'd like to accomplish: I have two numpy arrays: x contains numbers between 1 and 366 (but not necessarily all of them), representing days of the year 2016. 'y' contains some other number. I'd like to make a scatter plot of 'y' versus 'x':

import numpy as np
import matplotlib.pyplot as plt
x = np.array([27, 38, 100, 300])
y = np.array([0.5, 2.5, 1.0, 0.8])
plt.scatter(x, y)

不出所料,这会生成一个刻度为 0、50、100、...、350 的图表.我想将这些刻度标签更改为单独的日期.(例如,在50处的刻度将被标记为"2月19日"之类的东西.)假设我有一个函数 tick_to_date ,该函数可以将数字0转换为日期字符串,因此我很容易手动更改图表中的所有刻度线.(如果你需要一个占位符函数:tick_to_date = lambda x:("day" + str(x)))

Unsurprisingly, this generates a graph with ticks at 0, 50, 100, ..., 350. I'd like to change these tick labels to individual dates. (E.g. the tick at 50 would be labeled something like 'February 19'.) Suppose I have a function tick_to_date which can transform the number 0 into a date string, so it'd be easy for me to manually change all the ticks in my graph. (If you need a placeholder function: tick_to_date = lambda x: ("day " + str(x)))

ax = plt.gca()
ax.set_xticklabels([tick_to_date(tick.get_text()) for tick in ax.get_xticklabels()])

但是,这只执行一次.如果我现在放大或采取任何改变刻度的操作,新的刻度标签将不会是我想要的样子.

However, this only does it once. If I now zoom in, or undertake any action that changes the ticks, the new tick labels aren't going to be what I want them to be.

理想情况下,我告诉轴始终使用自己的 tick_to_date 函数变换刻度标签,而不是手动设置标签.或者,每次更改刻度时都调用上面的代码行,但我不确定这是否会奏效.这些可能/可行/令人愉快吗?

Ideally, instead of setting the labels manually, I'd tell the axis to always transform the tick labels with my own tick_to_date function. Alternatively, call the above line of code every time the ticks are changed, but I'm not sure that would work so well. Is either of these possible/feasible/pleasantly available?

推荐答案

如果我真的理解你的问题,你正在寻找 函数格式化程序来自 matplotlib.ticker:

If I truly understand your question you are looking for function formatter from matplotlib.ticker:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import ticker

# I added 'y' to fit second argument (position) of FuncFormatter
tick_to_date = lambda x,y: ("day " + str(x))

x = np.array([27, 38, 100, 300])
y = np.array([0.5, 2.5, 1.0, 0.8])
plt.scatter(x, y)

ax = plt.gca()
# tick_to_date will affect all tick labels through MyFormatter
myFormatter = ticker.FuncFormatter(tick_to_date)
# apply formatter for selected axis
ax.xaxis.set_major_formatter(myFormatter)
plt.show()

这篇关于Matplotlib:自动修改轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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