Surface and 3d contour in matplotlib

Apparently it is a bug, if you try this import numpy as np from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111, projection=”3d”) X, Y = np.mgrid[-1:1:30j, -1:1:30j] Z = np.sin(np.pi*X)*np.sin(np.pi*Y) ax.plot_surface(X, Y, Z, cmap=”autumn_r”, lw=0, rstride=1, cstride=1) ax.contour(X, Y, Z+1, 10, lw=3, colors=”k”, linestyles=”solid”) plt.show() And rotate around, you … Read more

Cannot load backend ‘Qt5Agg’ which requires the ‘qt5’ interactive framework, as ‘headless’ is currently running

This is happening because Google Colab and Jupyter run on virtual environments which do not support GUI outputs as you cannot open new windows through a browser. Running it locally on a code editor(Spyder, or even IDLE) ensures that it can open a new window for the GUI to initialize. For coding the GUI it … Read more

change scatter plot marker thickness

you are looking for the kwarg linewidths. e.g.: import matplotlib.pyplot as plt import numpy as np x = y = np.arange(5) fig,ax = plt.subplots(1) ax.scatter(x,y, s=100,marker=”x”,color=”b”,linewidths=1) ax.scatter(x,y+1,s=100,marker=”x”,color=”r”,linewidths=2) ax.scatter(x,y+2,s=100,marker=”x”,color=”g”,linewidths=3) plt.show() Note: On some versions of matplotlib, it appears the kwarg is linewidth, not linewidths, despite what the manual currently says (April 2020). This is a known … Read more

seaborn is not plotting within defined subplots

seaborn.distplot has been DEPRECATED in seaborn 0.11 and is replaced with the following: displot(), a figure-level function with a similar flexibility over the kind of plot to draw. This is a FacetGrid, and does not have the ax parameter, so it will not work with matplotlib.pyplot.subplots. histplot(), an axes-level function for plotting histograms, including with … Read more

Getting information for bins from the histogram function

The return values of plt.hist are: Returns: tuple : (n, bins, patches) or ([n0, n1, …], bins, [patches0, patches1,…]) So all you need to do is capture the return values appropriately. For example: import numpy as np import matplotlib.pyplot as plt # generate some uniformly distributed data x = np.random.rand(1000) # create the histogram (n, … Read more

figsize in matplotlib is not changing the figure size? [duplicate]

One option (as mentioned by @tda), and probably the best/most standard way, is to put the plt.figure before the plt.bar: import matplotlib.pyplot as plt plt.figure(figsize=(20,10)) plt.bar(x[‘user’], x[‘number’], color=”blue”) Another option, if you want to set the figure size after creating the figure, is to use fig.set_size_inches (note I used plt.gcf here to get the current … Read more

Python: A4 size for a plot

Try to set the size of the figure (in inches) before you save it. You can do this when you initialize the figure by doing: figure(figsize=(11.69,8.27)) # for landscape or if the figure exists: f = gcf() # f = figure(n) if you know the figure number f.set_size_inches(11.69,8.27) or in advance for all plots, using … Read more

Matplotlib Savefig will NOT overwrite old files

TLDR: The photos WERE being overwritten, but the date was kept the same as the original file, due to a quirk of windows when a folder has lots of photos. Jon’s answer from 10/2/2015 did the trick for me. https://superuser.com/questions/147525/what-is-the-date-column-in-windows-7-explorer-it-matches-no-date-column-from/335901#335901 Basically windows detects lots of pictures in a folder and “optimizes” said folder for pictures. … Read more