How to plot a scatter plot with a legend label for each class

Actually both linked questions provide a way how to achieve the desired result. The easiest method is to create as many scatter plots as unique classes exist and give each a single color and legend entry. import matplotlib.pyplot as plt x=[1,2,3,4] y=[5,6,7,8] classes = [2,4,4,2] unique = list(set(classes)) colors = [plt.cm.jet(float(i)/max(unique)) for i in unique] … Read more

How to remove xticks from a plot

The plt.tick_params method is very useful for stuff like this. This code turns off major and minor ticks and removes the labels from the x-axis. Note that there is also ax.tick_params for matplotlib.axes.Axes objects. from matplotlib import pyplot as plt plt.plot(range(10)) plt.tick_params( axis=”x”, # changes apply to the x-axis which=”both”, # both major and minor … Read more

Save plot to image file instead of displaying it

When using matplotlib.pyplot.savefig, the file format can be specified by the extension: from matplotlib import pyplot as plt plt.savefig(‘foo.png’) plt.savefig(‘foo.pdf’) That gives a rasterized or vectorized output respectively. In addition, there is sometimes undesirable whitespace around the image, which can be removed with: plt.savefig(‘foo.png’, bbox_inches=”tight”) Note that if showing the plot, plt.show() should follow plt.savefig(); … Read more

3D Plots over non-rectangular domain

Abritrary points can be supplied as 1D arrays to matplotlib.Axes3D.plot_trisurf. It doesn’t matter whether they follow a specific structure. Other methods which would depend on the structure of the data would be Interpolate the points on a regular rectangular grid. This can be accomplished using scipy.interpolate.griddata. See example here Reshape the input arrays such that … Read more