Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlapping y-axis tick label and x-axis tick label in matplotlib

If I create a plot with matplotlib using the following code:

import numpy as np from matplotlib import pyplot as plt xx = np.arange(0,5, .5) yy = np.random.random( len(xx) ) plt.plot(xx,yy) plt.imshow() 

I get a result that looks like the attached image. The problem is the bottom-most y-tick label overlaps the left-most x-tick label. This looks unprofessional. I was wondering if there was an automatic way to delete the bottom-most y-tick label, so I don't have the overlap problem. The fewer lines of code, the better. enter image description here

like image 869
ncRubert Avatar asked Feb 23 '12 22:02

ncRubert


People also ask

How do you avoid overlapping x-axis labels in MatPlotLib?

Matplotlib x-axis label overlap In matplotlib, we have a method setp() that is used to set the rotation and alignment attributes of tick labels to avoid overlapping. To get ticklabels, we use the plt. setp() and get.

How do I stop MatPlotLib overlapping?

Use legend() method to avoid overlapping of labels and autopct. To display the figure, use show() method.

How do you prevent x-axis labels from overlapping?

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.

How do I add X and Y axis labels in MatPlotLib?

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


1 Answers

In the ticker module there is a class called MaxNLocator that can take a prune kwarg.
Using that you can remove the first tick:

import matplotlib.pyplot as plt from matplotlib.ticker import MaxNLocator import numpy as np xx = np.arange(0,5, .5) yy = np.random.random( len(xx) ) plt.plot(xx,yy) plt.gca().xaxis.set_major_locator(MaxNLocator(prune='lower')) plt.show() 

Result:

enter image description here

like image 139
mechanical_meat Avatar answered Oct 13 '22 03:10

mechanical_meat