58

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.

Sasha
  • 1,338
  • 2
  • 13
  • 22

4 Answers4

79

Use the xticks command.

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()
David Jones
  • 4,766
  • 3
  • 32
  • 45
Stephen Terry
  • 6,169
  • 1
  • 28
  • 31
  • 2
    Why'd you use `len(t12)` in both lines? Shouldn't one be `len(t11)`? – User Sep 22 '16 at 01:31
  • @User Generally yes, you're right. When calling `xticks` you typically want the number of ticks to be equal to the number of labels. In this case though the length of both lists is the same so it doesn't really matter. – hoffee Jul 13 '18 at 17:05
  • 3
    `bar` has an argument `tick_label` which can be used instead of `plt.xticks`, i.e. `plt.bar(..., tick_label=t11)`. – luator Dec 16 '20 at 15:45
50

For the object oriented API of matplotlib one can plot custom text on the x-ticks of an axis with following code:

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

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

David Alvarez
  • 1,370
  • 1
  • 13
  • 12
3

Firstly you need to access the axis object:

fig, ax = plt.subplots(1,1)

then:

ax.set_yticks([x for x in range(-10,11)])
ax.set_yticklabels( ['{0:2d}'.format(abs(x)) for x in range(-10, 11)])
Colin Dickie
  • 910
  • 4
  • 9
3

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

Community
  • 1
  • 1
Ludovico Fischer
  • 1,602
  • 11
  • 11