Get more time points on x axis of a spectrogram












0















So I've represented a spectrogram for a relatively long video(15 minutes). On the time axis, I've got data points at each 3 minute and 20 seconds.
This is my spectrogram:
enter image description here



I would like to have data point on the x axis for every second. I've tries to modify the function timeTicks from the code, but it doesn't work. As an alternative, I've seen that I could zoom in on my spectrogram, but I can't seem to get it to work.



This is the code:



import matplotlib.pyplot as plt
import scipy.io.wavfile as wavfile

cmap = plt.get_cmap('plasma') # this may fail on older versions of matplotlib
vmin = -40 # hide anything below -40 dB
cmap.set_under(color='k', alpha=None)

rate, frames = wavfile.read("audio_test.wav")
fig, ax = plt.subplots()
pxx, freq, t, cax = ax.specgram(frames[:, 0], # first channel
Fs=rate, # to get frequency axis in Hz
cmap=cmap, vmin=vmin)
cbar = fig.colorbar(cax)
cbar.set_label('Intensity dB')
ax.axis("tight")

# Prettify
import matplotlib
import datetime

ax.set_xlabel('time h:mm:ss')
ax.set_ylabel('frequency kHz')

scale = 1e3 # KHz
ticks = matplotlib.ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x/scale))
ax.yaxis.set_major_formatter(ticks)

def timeTicks(x, pos):
d = datetime.timedelta(seconds=x)
return str(d)
formatter = matplotlib.ticker.FuncFormatter(timeTicks)
ax.xaxis.set_major_formatter(formatter)
ax.xaxis.set_major_locator(ticker.IndexLocator(base=180, offset=60))
plt.show()


Edit:
The present spectogram:
enter image description here










share|improve this question

























  • set_major_formatter only controls how each tick label is written. To control where the ticks are, you need to set_major_locator with a tick locator from matplotlib.dates.

    – Joooeey
    Nov 23 '18 at 0:08











  • and if you don't want labels on all your ticks, use set_minor_locator. A label on every second would be too much.

    – Joooeey
    Nov 23 '18 at 0:09











  • Thank you for your response @Joooeey! Can't seem to get it done... I've added this line of code: "ax.xaxis.set_major_locator(ticker.IndexLocator(base=180, offset=60))". I have a weird value after the seconds on the x value. Something like ".001415" and I don't know how to get rid of it. I'll attach a new picture with the present spectrogram.

    – PyRar
    Nov 23 '18 at 5:36











  • Try: from matplotlib import dates and then ax.xaxis.set_major_locator(dates.MinuteLocator(interval=2)) and ax.xaxis.set_minor_locator(dates.SecondLocator()).

    – Joooeey
    Nov 23 '18 at 17:00













  • I don't know what the issue with the IndexLocator is, but you can hide the problem by not displaying fractions of a second: ax.xaxis.set_major_formatter(dates.DateFormatter('%H:%M:%S')

    – Joooeey
    Nov 23 '18 at 17:06
















0















So I've represented a spectrogram for a relatively long video(15 minutes). On the time axis, I've got data points at each 3 minute and 20 seconds.
This is my spectrogram:
enter image description here



I would like to have data point on the x axis for every second. I've tries to modify the function timeTicks from the code, but it doesn't work. As an alternative, I've seen that I could zoom in on my spectrogram, but I can't seem to get it to work.



This is the code:



import matplotlib.pyplot as plt
import scipy.io.wavfile as wavfile

cmap = plt.get_cmap('plasma') # this may fail on older versions of matplotlib
vmin = -40 # hide anything below -40 dB
cmap.set_under(color='k', alpha=None)

rate, frames = wavfile.read("audio_test.wav")
fig, ax = plt.subplots()
pxx, freq, t, cax = ax.specgram(frames[:, 0], # first channel
Fs=rate, # to get frequency axis in Hz
cmap=cmap, vmin=vmin)
cbar = fig.colorbar(cax)
cbar.set_label('Intensity dB')
ax.axis("tight")

# Prettify
import matplotlib
import datetime

ax.set_xlabel('time h:mm:ss')
ax.set_ylabel('frequency kHz')

scale = 1e3 # KHz
ticks = matplotlib.ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x/scale))
ax.yaxis.set_major_formatter(ticks)

def timeTicks(x, pos):
d = datetime.timedelta(seconds=x)
return str(d)
formatter = matplotlib.ticker.FuncFormatter(timeTicks)
ax.xaxis.set_major_formatter(formatter)
ax.xaxis.set_major_locator(ticker.IndexLocator(base=180, offset=60))
plt.show()


Edit:
The present spectogram:
enter image description here










share|improve this question

























  • set_major_formatter only controls how each tick label is written. To control where the ticks are, you need to set_major_locator with a tick locator from matplotlib.dates.

    – Joooeey
    Nov 23 '18 at 0:08











  • and if you don't want labels on all your ticks, use set_minor_locator. A label on every second would be too much.

    – Joooeey
    Nov 23 '18 at 0:09











  • Thank you for your response @Joooeey! Can't seem to get it done... I've added this line of code: "ax.xaxis.set_major_locator(ticker.IndexLocator(base=180, offset=60))". I have a weird value after the seconds on the x value. Something like ".001415" and I don't know how to get rid of it. I'll attach a new picture with the present spectrogram.

    – PyRar
    Nov 23 '18 at 5:36











  • Try: from matplotlib import dates and then ax.xaxis.set_major_locator(dates.MinuteLocator(interval=2)) and ax.xaxis.set_minor_locator(dates.SecondLocator()).

    – Joooeey
    Nov 23 '18 at 17:00













  • I don't know what the issue with the IndexLocator is, but you can hide the problem by not displaying fractions of a second: ax.xaxis.set_major_formatter(dates.DateFormatter('%H:%M:%S')

    – Joooeey
    Nov 23 '18 at 17:06














0












0








0








So I've represented a spectrogram for a relatively long video(15 minutes). On the time axis, I've got data points at each 3 minute and 20 seconds.
This is my spectrogram:
enter image description here



I would like to have data point on the x axis for every second. I've tries to modify the function timeTicks from the code, but it doesn't work. As an alternative, I've seen that I could zoom in on my spectrogram, but I can't seem to get it to work.



This is the code:



import matplotlib.pyplot as plt
import scipy.io.wavfile as wavfile

cmap = plt.get_cmap('plasma') # this may fail on older versions of matplotlib
vmin = -40 # hide anything below -40 dB
cmap.set_under(color='k', alpha=None)

rate, frames = wavfile.read("audio_test.wav")
fig, ax = plt.subplots()
pxx, freq, t, cax = ax.specgram(frames[:, 0], # first channel
Fs=rate, # to get frequency axis in Hz
cmap=cmap, vmin=vmin)
cbar = fig.colorbar(cax)
cbar.set_label('Intensity dB')
ax.axis("tight")

# Prettify
import matplotlib
import datetime

ax.set_xlabel('time h:mm:ss')
ax.set_ylabel('frequency kHz')

scale = 1e3 # KHz
ticks = matplotlib.ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x/scale))
ax.yaxis.set_major_formatter(ticks)

def timeTicks(x, pos):
d = datetime.timedelta(seconds=x)
return str(d)
formatter = matplotlib.ticker.FuncFormatter(timeTicks)
ax.xaxis.set_major_formatter(formatter)
ax.xaxis.set_major_locator(ticker.IndexLocator(base=180, offset=60))
plt.show()


Edit:
The present spectogram:
enter image description here










share|improve this question
















So I've represented a spectrogram for a relatively long video(15 minutes). On the time axis, I've got data points at each 3 minute and 20 seconds.
This is my spectrogram:
enter image description here



I would like to have data point on the x axis for every second. I've tries to modify the function timeTicks from the code, but it doesn't work. As an alternative, I've seen that I could zoom in on my spectrogram, but I can't seem to get it to work.



This is the code:



import matplotlib.pyplot as plt
import scipy.io.wavfile as wavfile

cmap = plt.get_cmap('plasma') # this may fail on older versions of matplotlib
vmin = -40 # hide anything below -40 dB
cmap.set_under(color='k', alpha=None)

rate, frames = wavfile.read("audio_test.wav")
fig, ax = plt.subplots()
pxx, freq, t, cax = ax.specgram(frames[:, 0], # first channel
Fs=rate, # to get frequency axis in Hz
cmap=cmap, vmin=vmin)
cbar = fig.colorbar(cax)
cbar.set_label('Intensity dB')
ax.axis("tight")

# Prettify
import matplotlib
import datetime

ax.set_xlabel('time h:mm:ss')
ax.set_ylabel('frequency kHz')

scale = 1e3 # KHz
ticks = matplotlib.ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x/scale))
ax.yaxis.set_major_formatter(ticks)

def timeTicks(x, pos):
d = datetime.timedelta(seconds=x)
return str(d)
formatter = matplotlib.ticker.FuncFormatter(timeTicks)
ax.xaxis.set_major_formatter(formatter)
ax.xaxis.set_major_locator(ticker.IndexLocator(base=180, offset=60))
plt.show()


Edit:
The present spectogram:
enter image description here







python matplotlib spectrogram






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 5:37







PyRar

















asked Nov 22 '18 at 22:44









PyRarPyRar

1157




1157













  • set_major_formatter only controls how each tick label is written. To control where the ticks are, you need to set_major_locator with a tick locator from matplotlib.dates.

    – Joooeey
    Nov 23 '18 at 0:08











  • and if you don't want labels on all your ticks, use set_minor_locator. A label on every second would be too much.

    – Joooeey
    Nov 23 '18 at 0:09











  • Thank you for your response @Joooeey! Can't seem to get it done... I've added this line of code: "ax.xaxis.set_major_locator(ticker.IndexLocator(base=180, offset=60))". I have a weird value after the seconds on the x value. Something like ".001415" and I don't know how to get rid of it. I'll attach a new picture with the present spectrogram.

    – PyRar
    Nov 23 '18 at 5:36











  • Try: from matplotlib import dates and then ax.xaxis.set_major_locator(dates.MinuteLocator(interval=2)) and ax.xaxis.set_minor_locator(dates.SecondLocator()).

    – Joooeey
    Nov 23 '18 at 17:00













  • I don't know what the issue with the IndexLocator is, but you can hide the problem by not displaying fractions of a second: ax.xaxis.set_major_formatter(dates.DateFormatter('%H:%M:%S')

    – Joooeey
    Nov 23 '18 at 17:06



















  • set_major_formatter only controls how each tick label is written. To control where the ticks are, you need to set_major_locator with a tick locator from matplotlib.dates.

    – Joooeey
    Nov 23 '18 at 0:08











  • and if you don't want labels on all your ticks, use set_minor_locator. A label on every second would be too much.

    – Joooeey
    Nov 23 '18 at 0:09











  • Thank you for your response @Joooeey! Can't seem to get it done... I've added this line of code: "ax.xaxis.set_major_locator(ticker.IndexLocator(base=180, offset=60))". I have a weird value after the seconds on the x value. Something like ".001415" and I don't know how to get rid of it. I'll attach a new picture with the present spectrogram.

    – PyRar
    Nov 23 '18 at 5:36











  • Try: from matplotlib import dates and then ax.xaxis.set_major_locator(dates.MinuteLocator(interval=2)) and ax.xaxis.set_minor_locator(dates.SecondLocator()).

    – Joooeey
    Nov 23 '18 at 17:00













  • I don't know what the issue with the IndexLocator is, but you can hide the problem by not displaying fractions of a second: ax.xaxis.set_major_formatter(dates.DateFormatter('%H:%M:%S')

    – Joooeey
    Nov 23 '18 at 17:06

















set_major_formatter only controls how each tick label is written. To control where the ticks are, you need to set_major_locator with a tick locator from matplotlib.dates.

– Joooeey
Nov 23 '18 at 0:08





set_major_formatter only controls how each tick label is written. To control where the ticks are, you need to set_major_locator with a tick locator from matplotlib.dates.

– Joooeey
Nov 23 '18 at 0:08













and if you don't want labels on all your ticks, use set_minor_locator. A label on every second would be too much.

– Joooeey
Nov 23 '18 at 0:09





and if you don't want labels on all your ticks, use set_minor_locator. A label on every second would be too much.

– Joooeey
Nov 23 '18 at 0:09













Thank you for your response @Joooeey! Can't seem to get it done... I've added this line of code: "ax.xaxis.set_major_locator(ticker.IndexLocator(base=180, offset=60))". I have a weird value after the seconds on the x value. Something like ".001415" and I don't know how to get rid of it. I'll attach a new picture with the present spectrogram.

– PyRar
Nov 23 '18 at 5:36





Thank you for your response @Joooeey! Can't seem to get it done... I've added this line of code: "ax.xaxis.set_major_locator(ticker.IndexLocator(base=180, offset=60))". I have a weird value after the seconds on the x value. Something like ".001415" and I don't know how to get rid of it. I'll attach a new picture with the present spectrogram.

– PyRar
Nov 23 '18 at 5:36













Try: from matplotlib import dates and then ax.xaxis.set_major_locator(dates.MinuteLocator(interval=2)) and ax.xaxis.set_minor_locator(dates.SecondLocator()).

– Joooeey
Nov 23 '18 at 17:00







Try: from matplotlib import dates and then ax.xaxis.set_major_locator(dates.MinuteLocator(interval=2)) and ax.xaxis.set_minor_locator(dates.SecondLocator()).

– Joooeey
Nov 23 '18 at 17:00















I don't know what the issue with the IndexLocator is, but you can hide the problem by not displaying fractions of a second: ax.xaxis.set_major_formatter(dates.DateFormatter('%H:%M:%S')

– Joooeey
Nov 23 '18 at 17:06





I don't know what the issue with the IndexLocator is, but you can hide the problem by not displaying fractions of a second: ax.xaxis.set_major_formatter(dates.DateFormatter('%H:%M:%S')

– Joooeey
Nov 23 '18 at 17:06












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438758%2fget-more-time-points-on-x-axis-of-a-spectrogram%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438758%2fget-more-time-points-on-x-axis-of-a-spectrogram%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Ottavio Pratesi

Tricia Helfer

15 giugno