在 matplotlib 散点图中自定义 x 和 y 标签
Customize x and y labels in matplotlib scatter plot
我有两个长度相等的列表 xs
和 ys
用于绘制散点图:
import random
import matplotlib.pyplot as plt
xs = [random.randrange(0,100) for i in range(50)]
ys = [random.randrange(0,100) for i in range(50)]
plt.scatter(xs,ys)
但是,我不想要标准轴标签,而是从中推断出的标签,例如以下词典:
x_labels = { 40 : "First", 52 : "Second", 73: "Third" , 99: "Forth" }
y_labels = { 10 : "FIRST", 80 : "SECOND" }
所以我想做的是绘制一个散点图,标签为 "First" at x = 40,"Second" at x = 73 等等,以及 "FIRST" 在 y = 10 和 "SECOND" 在 y = 80。不幸的是,我还没有找到实现这个的方法。
非常感谢!
要在所需位置显示刻度标签,您可以使用:
plt.xticks(list(x_labels.keys()), x_labels.values())
plt.yticks(list(y_labels.keys()), y_labels.values())
如您所述,这会导致状态栏中不再显示坐标。
获得显示坐标和自定义刻度的解决方法是使用 custom tick formattter。这样的格式化程序有两个参数:一个 x
值和一个 pos
。 pos
在状态栏中显示坐标时为 None,但为刻度标签设置。因此,检查 pos
而不是 None,格式化程序可以 return 所需的标签,否则可以 return 编辑格式化为字符串的数字。刻度位置仍然需要通过 plt.xticks()
设置,而不是标签。
这是一个例子:
import random
import matplotlib.pyplot as plt
from matplotlib import ticker
@ticker.FuncFormatter
def major_x_formatter(x, pos):
if pos is not None:
return f"{x_labels_list[pos]}"
x_r = int(round(x))
if x_r in x_labels:
return f"{x:.0f}:{x_labels[x_r]}"
else:
return f"{x:.2f}"
@ticker.FuncFormatter
def major_y_formatter(y, pos):
if pos is not None:
return f"{y_labels_list[pos]}"
y_r = int(round(y))
if y_r in y_labels:
return f"{y:.0f}:{y_labels[y_r]}"
else:
return f"{y:.2f}"
xs = [random.randrange(0,100) for i in range(50)]
ys = [random.randrange(0,100) for i in range(50)]
plt.scatter(xs,ys)
x_labels = { 40 : "First", 52 : "Second", 73: "Third" , 99: "Forth" }
x_labels_list = list(x_labels.values())
y_labels = { 10 : "FIRST", 80 : "SECOND" }
y_labels_list = list(y_labels.values())
plt.xticks(list(x_labels.keys()))
plt.yticks(list(y_labels.keys()))
plt.gca().xaxis.set_major_formatter(major_x_formatter)
plt.gca().yaxis.set_major_formatter(major_y_formatter)
plt.show()
我有两个长度相等的列表 xs
和 ys
用于绘制散点图:
import random
import matplotlib.pyplot as plt
xs = [random.randrange(0,100) for i in range(50)]
ys = [random.randrange(0,100) for i in range(50)]
plt.scatter(xs,ys)
但是,我不想要标准轴标签,而是从中推断出的标签,例如以下词典:
x_labels = { 40 : "First", 52 : "Second", 73: "Third" , 99: "Forth" }
y_labels = { 10 : "FIRST", 80 : "SECOND" }
所以我想做的是绘制一个散点图,标签为 "First" at x = 40,"Second" at x = 73 等等,以及 "FIRST" 在 y = 10 和 "SECOND" 在 y = 80。不幸的是,我还没有找到实现这个的方法。
非常感谢!
要在所需位置显示刻度标签,您可以使用:
plt.xticks(list(x_labels.keys()), x_labels.values())
plt.yticks(list(y_labels.keys()), y_labels.values())
如您所述,这会导致状态栏中不再显示坐标。
获得显示坐标和自定义刻度的解决方法是使用 custom tick formattter。这样的格式化程序有两个参数:一个 x
值和一个 pos
。 pos
在状态栏中显示坐标时为 None,但为刻度标签设置。因此,检查 pos
而不是 None,格式化程序可以 return 所需的标签,否则可以 return 编辑格式化为字符串的数字。刻度位置仍然需要通过 plt.xticks()
设置,而不是标签。
这是一个例子:
import random
import matplotlib.pyplot as plt
from matplotlib import ticker
@ticker.FuncFormatter
def major_x_formatter(x, pos):
if pos is not None:
return f"{x_labels_list[pos]}"
x_r = int(round(x))
if x_r in x_labels:
return f"{x:.0f}:{x_labels[x_r]}"
else:
return f"{x:.2f}"
@ticker.FuncFormatter
def major_y_formatter(y, pos):
if pos is not None:
return f"{y_labels_list[pos]}"
y_r = int(round(y))
if y_r in y_labels:
return f"{y:.0f}:{y_labels[y_r]}"
else:
return f"{y:.2f}"
xs = [random.randrange(0,100) for i in range(50)]
ys = [random.randrange(0,100) for i in range(50)]
plt.scatter(xs,ys)
x_labels = { 40 : "First", 52 : "Second", 73: "Third" , 99: "Forth" }
x_labels_list = list(x_labels.values())
y_labels = { 10 : "FIRST", 80 : "SECOND" }
y_labels_list = list(y_labels.values())
plt.xticks(list(x_labels.keys()))
plt.yticks(list(y_labels.keys()))
plt.gca().xaxis.set_major_formatter(major_x_formatter)
plt.gca().yaxis.set_major_formatter(major_y_formatter)
plt.show()