matplotlib字符串作为x轴上的标签 [英] matplotlib strings as labels on x axis

查看:87
本文介绍了matplotlib字符串作为x轴上的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个用于数据分析的小工具,现在我不得不绘制准备好的数据.之前的代码将产生以下两个长度相等的列表.

I am building a small tool for data analysis and I have come to the point, where I have to plot the prepared data. The code before this produces the following two lists with equal length.

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建立一个直方图.但是,有两个问题: 1.所有x均连接了t11 [x]和t12 [x].其中t11 [x]实际上是一个字符串.它代表某种检测器组合.例如:'01'表示检测是在第一个检测器的第0段和第二个检测器的第一个段中进行的.我的目标是让t11中的每个条目都作为x轴上的标记点. t12条目将定义t11条目上方的条形图的高点(在对数y轴上)

Based on this, I want to built a histogram with matplotlib.plt.hist. However, there are a couple of problems: 1. t11[x] and t12[x] are connected for all x. Where t11[x] is actually a string. It represents a certain detector combination. For example: '01' tells that the detection was made in the 0th segment of the first detector, and 1st segment of the second detector. My goal is to have each entry from t11 as a labeled point on the x axis. The t12 entry is going to define the hight of the bar above the t11 entry (on a logarithmic y axis)

如何配置这样的x轴? 2.这对我来说是全新的.我在文档中找不到任何相关内容.最可能是因为我不知道要搜索什么. SO:我要实现的目标是否有一个正式"名称.这也对我有很大帮助.

How does one configure such an x axis? 2. This is all very new to me. I could not find anything related in the documentation. Most probably because I did not know what to search for. SO: Is there an "official" name for what I am trying to achieve. This would also help me alot.

推荐答案

使用 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()

这篇关于matplotlib字符串作为x轴上的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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