Rui Huang
Rui Huang

Reputation: 382

How to make waveplot nicely displayed using librosa with matplotlib

Referring to this link: https://aqibsaeed.github.io/2016-09-03-urban-sound-classification-part-1/, I am trying to make the same waveplot figure, however, I got the figure as this: Waveplot.

I am running these python codes:

def plot_waves(sound_names, raw_sounds):
    i = 1
    fig = plt.figure()
    for n, f in zip(sound_names, raw_sounds):
        plt.subplot(10, 1, i)
        librosa.display.waveplot(np.array(f), sr=22050)
        plt.title(n.title())
        i += 1
    plt.suptitle("Figure 1: Waveplot", x=0.5, y=0.915, fontsize=18)
    plt.show()

Any idea how to improve to achieve the same figure as shown in the link? Thanks.

Upvotes: 1

Views: 4506

Answers (1)

Brian
Brian

Reputation: 468

From the link you posted, the plot_waves function is copied below.

Note the figsize and dpi parameters passed to plt.figure. The figsize=(25,60) specifies the (width, height) of the figure. dpi of course specifies the resolution. Using these parameters will increase your figure size and resolve the "squished" look that seems to be the problem.

Since your figure title overlaps your first axis title, you'll also want to specify parameters for the position of the title in plt.suptitle. Not that the x and y values in plt.suptitle are fractions of the image size from the left and bottom, respectively. So x=0.5 specifies a title position in the middle of the figure horizontally and y=0.915 specifies a vertical position of 91.5% of the total figure height from the bottom of the figure.

Always feel free to play with these settings until you get a plot that looks right to you.

def plot_waves(sound_names, raw_sounds):
    i = 1
    fig = plt.figure(figsize=(25,60), dpi = 900)
    for n,f in zip(sound_names,raw_sounds):
        plt.subplot(10,1,i)
        librosa.display.waveplot(np.array(f),sr=22050)
        plt.title(n.title())
        i += 1
    plt.suptitle("Figure 1: Waveplot",x=0.5, y=0.915,fontsize=18)
    plt.show()

Upvotes: 2

Related Questions