TypeError: Invalid dimensions for image data when plotting array with imshow()

There is a (somewhat) related question on StackOverflow: Showing an image with pylab.imshow() Here the problem was that an array of shape (nx,ny,1) is still considered a 3D array, and must be squeezed or sliced into a 2D array. More generally, the reason for the Exception TypeError: Invalid dimensions for image data is shown here: matplotlib.pyplot.imshow() needs a … Read more

How to change plot background color?

I am making a scatter plot in matplotlib and need to change the background of the actual plot to black. I know how to change the face color of the plot using: My issue is that this changes the color of the space around the plot. How to I change the actual background color of … Read more

How to make a histogram from a list of data

do you have any idea how to make 200 evenly spaced out bins, and have your program store the data in the appropriate bins? You can, for example, use NumPy’s arange for a fixed bin size (or Python’s standard range object), and NumPy’s linspace for evenly spaced bins. Here are 2 simple examples from my matplotlib gallery Fixed bin size … Read more

How to update a plot in matplotlib?

You essentially have two options: Do exactly what you’re currently doing, but call graph1.clear() and graph2.clear() before replotting the data. This is the slowest, but most simplest and most robust option. Instead of replotting, you can just update the data of the plot objects. You’ll need to make some changes in your code, but this should be much, much … Read more

scipy.signal.spectrogram compared to matplotlib.pyplot.specgram

The following code generates a spectrogram using either scipy.signal.spectrogram or matplotlib.pyplot.specgram. The color contrast of the specgram function is, however, rather low. Is there a way to increase it? Using matplotlib.pyplot.specgram gives the following result: Using scipy.signal.spectrogram gives the following plot Both functions seem to use the ‘jet’ colormap. I would also be generally interested in the difference between the two functions. Although they … Read more