matplotlib / matplotlib/matplotlib

[ENH]: Automatically trim the time on the x axis

未关闭
#28,158 20 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

New feature status: duplicate topic: date handling topic: ticks axis labels
主要语言
Python
星标
23.2k
派生
8.5k
平均合并
1 天 6 小时
30 天内合并 PR
66

描述

### Problem

I would like to have the x-axis showing the timestamps of the samples of audio files. Here is a minimal example:

```
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import dates as mdates
from scipy.io import wavfile

# Open the WAV files
audio_1 = wavfile.read("audio1.wav")
freq_audio_1 = audio_1[0]
samples_1 = audio_1[1][:, 0] # Turn to mono

audio_2 = wavfile.read("audio2.wav")
freq_audio_2 = audio_2[0]
samples_2 = audio_2[1][:, 0] # Turn to mono

# Create the timestamps
t_audio_1 = np.arange(0, len(samples_1)) / freq_audio_1
t_audio_2 = np.arange(0, len(samples_2)) / freq_audio_2

# We turn them into datetime
t_audio_1 = np.array(t_audio_1*1000, dtype="datetime64[ms]")
t_audio_2 = np.array(t_audio_2*1000, dtype="datetime64[ms]")

# Create the figure
fig, ax = plt.subplots(1, 2, constrained_layout=True)

# If the audio files are more than 1 hour, we format as HH:MM:SS, else just MM:SS
if len(samples_1) / freq_audio_1 >= 3600 and len(samples_2) / freq_audio_2 >= 3600 :
formatter = mdates.AutoDateFormatter(mdates.AutoDateLocator(), defaultfmt='%H:%M:%S')
else:
formatter = mdates.AutoDateFormatter(mdates.AutoDateLocator(), defaultfmt='%M:%S')

plt.gcf().axes[0].xaxis.set_major_formatter(formatter)
plt.gcf().axes[1].xaxis.set_major_formatter(formatter)

ax[0].plot(t_audio_1, samples_1)
ax[1].plot(t_audio_2, samples_2)
plt.show()
```
Here is the output:
![Figure_11](https://github.com/matplotlib/matplotlib/assets/9433716/953eef6f-f764-413a-8995-73fd6300d079)

As you can see, the microsecond precision makes so that the ticks on the x axis are shown on top of each other.

### Proposed solution

The ideal would be to have the plot automatically decide how many significant digits after the comma are necessary, depending on the level of zoom (in a similar fashion to the way Audacity displays timestamps).

Thank you :)

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先重现最小音频时间戳示例,并读取 mdates.AutoDateFormatter 和 mdates.AutoDateLocator 在不同缩放级别下的行为。完成的标准是:x 轴通过选择适当的精度,自动避免时间戳标签重叠,同时仍能为较短和较长的音频范围显示有用的时间信息。

由索引模型根据 Issue 内容生成。

评估

技术栈
numpy, python
领域
data-visualization
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。