matplotlib字符串作为x轴上的标签

2024-06-08 12:47:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在构建一个用于数据分析的小工具,我已经到了必须绘制准备好的数据的地步。前面的代码生成以下两个长度相等的列表。

t11 = ['00', '01', '02', '03', '04', '05', '10', '11', '12', '13', '14', '15', '20', '21', '22', '23', '24', '25', '30', '31', '32', '33', '34', '35', '40', '41', '42', '43', '44', '45', '50', '51', '52', '53', '54', '55']

t12 = [173, 135, 141, 148, 140, 149, 152, 178, 135, 96, 109, 164, 137, 152, 172, 149, 93, 78, 116, 81, 149, 202, 172, 99, 134, 85, 104, 172, 177, 150, 130, 131, 111, 99, 143, 194]

基于此,我想用matplotlib.plt.hist构建一个直方图。但是,有两个问题: 一。所有x的t11[x]和t12[x]都是连接的,其中t11[x]实际上是一个字符串。它代表某种探测器组合。例如:“01”表示在第一个检测器的第0段和第二个检测器的第1段进行检测。我的目标是将t11中的每个条目作为x轴上的标记点。t12条目将定义t11条目上方的条的高度(在对数y轴上)

如何配置这样的x轴? 2。这对我来说都很新鲜。我在文件里找不到任何相关的东西。很可能是因为我不知道该找什么。所以:有没有一个“官方”的名字来描述我想要达到的目标。这对我也有很大帮助。


Tags: 工具数据目标列表matplotlib绘制条目plt
3条回答

对于matplotlib的面向对象API,可以使用以下代码在axisx-ticks上打印自定义文本:

x = np.arange(2,10,2)
y = x.copy()
x_ticks_labels = ['jan','feb','mar','apr','may']

fig, ax = plt.subplots(1,1) 
ax.plot(x,y)

# Set number of ticks for x-axis
ax.set_xticks(x)
# Set ticks labels for x-axis
ax.set_xticklabels(x_ticks_labels, rotation='vertical', fontsize=18)

enter image description here

在matplotlib行话中,您正在寻找设置自定义记号的方法。

pyplot.hist快捷方式似乎无法实现这一点。你需要逐步建立你的形象。这里已经有一个关于堆栈溢出的答案,它与您的问题非常相似,应该可以帮助您开始:Matplotlib - label each bin

使用xticks命令。

import matplotlib.pyplot as plt

t11 = ['00', '01', '02', '03', '04', '05', '10', '11', '12', '13', '14', '15',
       '20', '21', '22', '23', '24', '25', '30', '31', '32', '33', '34', '35',
       '40', '41', '42', '43', '44', '45', '50', '51', '52', '53', '54', '55']

t12 = [173, 135, 141, 148, 140, 149, 152, 178, 135, 96, 109, 164, 137, 152,
       172, 149, 93, 78, 116, 81, 149, 202, 172, 99, 134, 85, 104, 172, 177,
       150, 130, 131, 111, 99, 143, 194]


plt.bar(range(len(t12)), t12, align='center')
plt.xticks(range(len(t11)), t11, size='small')
plt.show()

相关问题 更多 >