简体   繁体   中英

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]). This shows exactly what I want and there is nothing I want to change besides the x axis labelling. 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.

[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: 小矩阵

大型矩阵

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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