Changing the “tick frequency” on x or y axis in matplotlib

You could explicitly set where you want to tick marks with plt.xticks: plt.xticks(np.arange(min(x), max(x)+1, 1.0)) For example, import numpy as np import matplotlib.pyplot as plt x = [0,5,9,10,15] y = [0,1,2,3,4] plt.plot(x,y) plt.xticks(np.arange(min(x), max(x)+1, 1.0)) plt.show() (np.arange was used rather than Python’s range function just in case min(x) and max(x) are floats instead of ints.) … Read more

How to have one colorbar for all subplots

Just place the colorbar in its own axis and use subplots_adjust to make room for it. As a quick example: import numpy as np import matplotlib.pyplot as plt fig, axes = plt.subplots(nrows=2, ncols=2) for ax in axes.flat: im = ax.imshow(np.random.random((10,10)), vmin=0, vmax=1) fig.subplots_adjust(right=0.8) cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7]) fig.colorbar(im, cax=cbar_ax) plt.show() Note that the … Read more

Difference in plotting with different matplotlib versions

The data is read in as strings. In matplotlib 2.0 those were automatically converted to floating point numbers such that they can be plotted. In matplotlib 2.1, categorical plots have been introduced. This now allows for something like plt.plot([“apple”, “banana”, “cherry”], [2,1,3]) While this is of course great for certain applications, it breaks the previous … Read more

Plot a function in python

Ok so here is what you want to do import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # for 3d plotting # constants h = 1.34e-34 m = 1.6e-19 a = 2e-9 # define our function (python standard uses lowercase for funcs/vars) def t(e, u): k2 = np.sqrt(2 * m * … Read more