Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib strings as labels on x axis

Tags:

matplotlib

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.

like image 435
Sasha Avatar asked Sep 26 '11 18:09

Sasha


People also ask

How do I add x-axis labels in Matplotlib?

Use the xlabel() method in matplotlib to add a label to the plot's x-axis.

How do you label the X-axis in Python?

With Pyplot, you can use the xlabel() and ylabel() functions to set a label for the x- and y-axis.

How do I set X tick labels?

To set string labels at x-axis tick labels, use set_xticklabels() function. To add suptitle, we use the suptitle() function of the figure class. To visualize the plot on the user's screen, use the show() function.

How do you prevent X-axis labels from overlapping each other?

0 A common problem in making plots, say a barplot or boxplot with a number of groups is that, names of the groups on x-axis label often overlap with each other. Till now, one of the solutions to avoid overlapping text x-axis is to swap x and y axis with coord_flip() and make a horizontal barplot or boxplot.


2 Answers

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()
like image 88
Stephen Terry Avatar answered Oct 16 '22 12:10

Stephen Terry


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

like image 42
David Alvarez Avatar answered Oct 16 '22 13:10

David Alvarez