matplotlib / matplotlib/matplotlib
[ENH]: Automatically trim the time on the x axis
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 23.2k
- Fork
- 8.5k
- Merge trung bình
- 1 ngày 6 giờ
- Pull request đã merge (30 ngày)
- 66
Mô tả
### 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:

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 :)
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách tái hiện ví dụ tối thiểu về timestamp âm thanh và đọc hành vi của mdates.AutoDateFormatter và mdates.AutoDateLocator ở các mức thu phóng khác nhau. Công việc được hoàn thành khi trục x tự động tránh các nhãn timestamp bị chồng lấp bằng cách chọn độ chính xác phù hợp, đồng thời vẫn hiển thị thông tin thời gian hữu ích cho các khoảng âm thanh ngắn và dài.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- numpy, python
- Lĩnh vực
- data-visualization
- Loại issue
- Tính năng
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100