Matplotlib table formatting

The matplotlib documentation says Add a table to the current axes. Returns a matplotlib.table.Table instance. For finer grained control over tables, use the Table class and add it to the axes with add_table(). You could do is the following, look at the properties of your table (it’s and object belonging to that class Table): print … Read more

How to set x axis ticklabels in a seaborn plot [duplicate]

Whenever you set the x-ticklabels manually, you should try to first set the corresponding ticks, and then specify the labels. In your case, therefore you should do g = sns.lineplot(data=df) g.set_xticks(range(len(df))) # <— set the ticks first g.set_xticklabels([‘2011′,’2012′,’2013′,’2014′,’2015′,’2016′,’2017′,’2018’])

Stacked Bar Chart with Centered Labels

The following method is more succinct, and easily scales. Putting the data into a pandas.DataFrame is the easiest way to plot a stacked bar plot. Using pandas.DataFrame.plot.bar(stacked=True), or pandas.DataFrame.plot(kind=’bar’, stacked=True), is the easiest way to plot a stacked bar plot. This method returns a matplotlib.axes.Axes or a numpy.ndarray of them. Since seaborn is just a … Read more

Hide contour linestroke on pyplot.contourf to get only fills

I finally found a proper solution to this long-standing problem (currently in Matplotlib 3), which does not require multiple calls to contour or rasterizing the figure. Note that the problem illustrated in the question appears only in saved publication-quality figures formats like PDF, not in lower-quality raster files like PNG. My solution was inspired by … Read more

How to convert a .wav file to a spectrogram in python3

Use scipy.signal.spectrogram. import matplotlib.pyplot as plt from scipy import signal from scipy.io import wavfile sample_rate, samples = wavfile.read(‘path-to-mono-audio-file.wav’) frequencies, times, spectrogram = signal.spectrogram(samples, sample_rate) plt.pcolormesh(times, frequencies, spectrogram) plt.imshow(spectrogram) plt.ylabel(‘Frequency [Hz]’) plt.xlabel(‘Time [sec]’) plt.show() Be sure that your wav file is mono (single channel) and not stereo (dual channel) before trying to do this. I highly … Read more

FacetGrid change titles

Although you can iterate through the axes and set the titles individually using matplotlib commands, it is cleaner to use seaborn’s built-in tools to control the title. For example: # Add a column of appropriate labels df_reduced[‘measure’] = df_reduced[‘ActualExternal’].replace({0: ‘Internal’, 1: ‘External’} g = sns.FacetGrid(df_reduced, col=”measure”, margin_titles=True) g.map(plt.hist, “ActualDepth”, color=”steelblue”, bins=bins, width=4.5) # Adjust title … Read more