axis
Restrict axis to integer tick locations
You can use the MaxNLocator method, like so: from pylab import MaxNLocator ya = axes.get_yaxis() ya.set_major_locator(MaxNLocator(integer=True))
Changing the color of an axis
When using figures, you can easily change the spine color with: ax.spines[‘bottom’].set_color(‘#dddddd’) ax.spines[‘top’].set_color(‘#dddddd’) ax.spines[‘right’].set_color(‘red’) ax.spines[‘left’].set_color(‘red’) Use the following to change only the ticks: which=”both” changes both the major and minor tick colors ax.tick_params(axis=”x”, colors=”red”) ax.tick_params(axis=”y”, colors=”red”) And the following to change only the label: ax.yaxis.label.set_color(‘red’) ax.xaxis.label.set_color(‘red’) And finally the title: ax.title.set_color(‘red’)
Rotate axis tick labels
This works for me: plt.xticks(rotation=90)
X-axis tick labels are too dense when drawing plots
A quick dirty solution would be the following: ax.set_xticks(ax.get_xticks()[::2]) This would only display every second xtick. If you wanted to only display every n-th tick you would use ax.set_xticks(ax.get_xticks()[::n]) If you don’t have a handle on ax you can get one as ax = plt.gca(). Alternatively, you could specify the number of xticks to use … Read more
Aligning y-ticks to the left
Tested in python 3.11.2, matplotlib 3.7.1 You will just need to add a pad. See matplotlib ticks position relative to axis yax = ax.get_yaxis() yax.set_tick_params(pad=pad) matplotlib.axis.Axis.set_tick_params To do your figuring of what the pad should be: import numpy as np import matplotlib.pyplot as plt ticks = [‘Lorem ipsum dolor sit amet, consectetur adipisicin’, ‘g elit, … Read more
How to set the ‘equal’ aspect ratio for all axes (x, y, z)
I like some of the previously posted solutions, but they do have the drawback that you need to keep track of the ranges and means over all your data. This could be cumbersome if you have multiple data sets that will be plotted together. To fix this, I made use of the ax.get_[xyz]lim3d() methods and … Read more
matlab multiple x axis one below another
Here is an example solution if you only need a second axis for showing a different scale (Jeff_K’s solution but more worked out): first_axis = gca; sqz = 0.12; %// distance to squeeze the first plot set(first_axis, ‘Position’, get(first_axis, ‘Position’) + [0 sqz 0 -sqz ]); ax2 = axes(‘Position’, get(first_axis, ‘Position’) .* [1 1 1 … Read more
Setting the position of the `ylabel`
You can move the ylabel using ax.yaxis.set_label_coords, which does accept negative numbers. For your example, I removed the line with set_label_position, and added: axPres.yaxis.set_label_coords(-0.1,1.02)