简体   繁体   English

使用matplotlib进行x轴标记

[英]x-axis labelling with matplotlib

I have a two dimensional (numpy)array and I plot the first column with the command plt.plot(wp[:, 0]). 我有一个二维(numpy)数组,并使用命令plt.plot(wp [:, 0])绘制第一列。 This shows exactly what I want and there is nothing I want to change besides the x axis labelling. 这恰好显示了我想要的东西,除了x轴标签外,我不需要更改。 For the x axis I am searching for a command which shows the area where the the value of the second column is the same and also which displays the y-number of this area. 对于x轴,我正在搜索一个命令,该命令显示第二列的值相同的区域,并且还显示该区域的y数。

[x1,y1]
[x2,y2]
[x3,y2]  
[x4,y3]
[x5,y3]
[x6,y3]
[x7,y4]

As u can the see in my example matrix, the entries in the second column are not unique but instead there are "regions" with the same value. 如您所见,在我的示例矩阵中,第二列中的条目不是唯一的,而是存在具有相同值的“区域”。

Edit: So plt.xticks(tx, wp[:,2], rotation='vertical')does work for smaller matrices but looks really ugly for larger ones: 编辑:所以plt.xticks(tx,wp [:,2],rotation ='vertical')确实适用于较小的矩阵,但对于较大的矩阵确实很丑陋: 小矩阵

大型矩阵

So in my opinion it would be enough if each number would just occur once. 因此,我认为如果每个数字仅出现一次就足够了。 Do you know how to do that? 你知道怎么做吗?

You'll have to: 您必须:

  • Customize the number of ticks 自定义刻度数
  • Customize what to print when for a certain value 自定义要打印的值时的值

Modified from the examples : 示例修改:

import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter, MaxNLocator
fig = plt.figure()
ax = fig.add_subplot(111)
xs = range(100)
ys = range(100)


def format_fn(tick_val, tick_pos):
    return '{0}'.format(int(tick_val))[:1]

ax.xaxis.set_major_formatter(FuncFormatter(format_fn))
ax.xaxis.set_major_locator(MaxNLocator(nbins=6,integer=True))
ax.plot(xs, ys)
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM