matplotlib 字符串作为 x 轴上的标签
我正在构建一个用于数据分析的小工具,现在我必须绘制准备好的数据。在此之前的代码生成以下两个长度相等的列表。
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 段和第二个检测器的第 1 段进行的。我的目标是将 t11 中的每个条目作为 x 轴上的标记点。 t12 条目将定义 t11 条目上方的条形高度(在对数 y 轴上)
如何配置这样的 x 轴? 2. 这对我来说都很新鲜。我在文档中找不到任何相关内容。很可能是因为我不知道要搜索什么。 SO:我想要实现的目标是否有一个“官方”名称?这也会对我有很大帮助。
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]
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)
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 xticks 命令。
Use the xticks command.
对于 matplotlib 的面向对象 API,可以使用以下代码在
axis
的x-ticks
上绘制自定义文本:For the object oriented API of matplotlib one can plot custom text on the
x-ticks
of anaxis
with following code:用 matplotlib 术语来说,您正在寻找一种设置自定义刻度的方法。
看来您无法使用 pyplot.hist 快捷方式实现此目的。相反,您将需要逐步构建您的形象。 Stack Overflow 上已经有一个与您的问题非常相似的答案,应该可以帮助您入门:Matplotlib - 为每个垃圾箱贴上标签
In matplotlib lingo, you are looking for a way to set custom ticks.
It seems you cannot achieve this with the
pyplot.hist
shortcut. You will need to build your image step by step instead. There is already an answer here on Stack Overflow to question which is very similar to yours and should get you started: Matplotlib - label each bin首先,您需要访问轴对象:
然后:
Firstly you need to access the axis object:
then: