I am having trouble with axes labels overlapping ticks labels in matplotlib. I've tried to reposition the labels "manually" by applying transforms or by calling set_y(), but no avail.
Here's a snippet that reproduces the problem:
import matplotlib matplotlib.use("TKAGG") import matplotlib.pyplot as pyplot import mpl_toolkits.mplot3d figure = pyplot.figure() figure.subplots_adjust(bottom=0.25, top=0.75) axes = figure.gca(projection='3d') xLabel = axes.set_xlabel('XXX xxxxxx xxxx x xx x') yLabel = axes.set_ylabel('YY (y) yyyyyy') zLabel = axes.set_zlabel('Z zzzz zzz (z)') plot = axes.plot([1,2,3],[1,2,3]) pyplot.show()
Note how the x and y labels clash with the ticks. Can I solve this elegantly ?
With matplotlib version 3.3. 0, the matplotlib functions set_xlabel and set_ylabel have a new parameter “loc” that can help adjust the positions of axis labels. For the x-axis label, it supports the values 'left', 'center', or 'right' to place the label towards left/center/right.
If we want to change the font size of the axis labels, we can use the parameter “fontsize” and set it your desired number.
xlabel() Function. The xlabel() function in pyplot module of matplotlib library is used to set the label for the x-axis.. Parameters: This method accept the following parameters that are described below: xlabel: This parameter is the label text.
Steps. Create a list of numbers (x) that can be used to tick the axes. Get the axis using subplot() that helps to add a subplot to the current figure. Set the ticks on X and Y axes using set_xticks and set_yticks methods respectively and list x (from step 1).
I share your frustration. I worked on it for a good half hour and got nowhere. The docs say set_xlabel takes an arg labelpad but I get an error (AttributeError: Unknown property labelpad)! Setting it after the fact doesn't do anything, on xaxis or w_xaxis.
Here's a crude workaround:
import matplotlib matplotlib.use("TKAGG") import matplotlib.pyplot as pyplot import mpl_toolkits.mplot3d figure = pyplot.figure(figsize=(8,4), facecolor='w') ax = figure.gca(projection='3d') xLabel = ax.set_xlabel('\nXXX xxxxxx xxxx x xx x', linespacing=3.2) yLabel = ax.set_ylabel('\nYY (y) yyyyyy', linespacing=3.1) zLabel = ax.set_zlabel('\nZ zzzz zzz (z)', linespacing=3.4) plot = ax.plot([1,2,3],[1,2,3]) ax.dist = 10 pyplot.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With