Add x and y labels to a pandas plot

The df.plot() function returns a matplotlib.axes.AxesSubplot object. You can set the labels on that object. ax = df2.plot(lw=2, colormap=’jet’, marker=”.”, markersize=10, title=”Video streaming dropout by category”) ax.set_xlabel(“x label”) ax.set_ylabel(“y label”) Or, more succinctly: ax.set(xlabel=”x label”, ylabel=”y label”). Alternatively, the index x-axis label is automatically set to the Index name, if it has one. so df2.index.name=”x … Read more

Moving matplotlib legend outside of the axis makes it cutoff by the figure box

Sorry EMS, but I actually just got another response from the matplotlib mailling list (Thanks goes out to Benjamin Root). The code I am looking for is adjusting the savefig call to: fig.savefig(‘samplefigure’, bbox_extra_artists=(lgd,), bbox_inches=”tight”) #Note that the bbox_extra_artists must be an iterable This is apparently similar to calling tight_layout, but instead you allow savefig … Read more

Removing white space around a saved image

You can remove the white space padding by setting bbox_inches=”tight” in savefig: plt.savefig(“test.png”,bbox_inches=”tight”) You’ll have to put the argument to bbox_inches as a string, perhaps this is why it didn’t work earlier for you. Possible duplicates: Matplotlib plots: removing axis, legends and white spaces How to set the margins for a matplotlib figure? Reduce left … Read more

How to set the subplot axis range

You have pylab.ylim: pylab.ylim([0,1000]) Note: The command has to be executed after the plot! Update 2021 Since the use of pylab is now strongly discouraged by matplotlib, you should instead use pyplot: from matplotlib import pyplot as plt plt.ylim(0, 100) #corresponding function for the x-axis plt.xlim(1, 1000)

Seaborn plots not showing up

Plots created using seaborn need to be displayed like ordinary matplotlib plots. This can be done using the plt.show() function from matplotlib. Originally I posted the solution to use the already imported matplotlib object from seaborn (sns.plt.show()) however this is considered to be a bad practice. Therefore, simply directly import the matplotlib.pyplot module and show … Read more

How can I convert an RGB image into grayscale in Python?

How about doing it with Pillow: from PIL import Image img = Image.open(‘image.png’).convert(‘L’) img.save(‘greyscale.png’) If an alpha (transparency) channel is present in the input image and should be preserved, use mode LA: img = Image.open(‘image.png’).convert(‘LA’) Using matplotlib and the formula Y’ = 0.2989 R + 0.5870 G + 0.1140 B you could do: import numpy … Read more