0.002917s 0.003735s 0.006754s 0.01487s 0.005988s 0...

تم الإنشاء في: ١٠ أبريل ٢٠٢٥

تمت الإجابة باستخدام O1 بواسطة Chat01

سؤال

0.002917s
0.003735s
0.006754s
0.01487s
0.005988s
0.013206s
0.020957s
0.038634s
0.060763s
0.084843s
0.44902s
1.165751s
转换成下面的格式
ifft_single = np.array([0.003177, 0.002598, 0.002255, 0.003032, 0.012379, 0.008505, 0.008486, 0.015785, 0.030393, 0.058895, 0.225113, 0.59084])

الفكرة

Translating times

I’m digging into converting times in seconds into a Python array assignment. The goal is to format them similarly to a given example, ensuring they’re truncated to 6 decimal places without 's'.

Finished reasoning

الإجابة

python
ifft_single = np.array([0.002917, 0.003735, 0.006754, 0.01487, 0.005988, 0.013206, 0.020957, 0.038634, 0.060763, 0.084843, 0.44902, 1.165751])

سؤال

plt.figure(figsize=(10, 6))
plt.plot(point_counts, fft_single, label='FFT 单精度', marker='o')
plt.plot(point_counts, fft_double, label='FFT 双精度', marker='o')
plt.plot(point_counts, ifft_single, label='IFFT 单精度', marker='s')
plt.plot(point_counts, ifft_double, label='IFFT 双精度', marker='s')
plt.xlabel('点数 N')
plt.ylabel('耗时(ms)')
plt.title('FFT/IFFT 耗时对比(单核)')
plt.legend()
plt.grid(True, which='both', linestyle='--')
plt.xscale('log', base=2)
plt.yscale('log')
plt.tight_layout()
plt.show()

这个折线图为什么刻度分布不均匀呢

الفكرة

Understanding uneven distribution

OK, let's think this through: the x-axis in the line chart uses a log scale with base 2, resulting in uneven tick spacing due to the logarithmic transformation.

Adjusting tick alignment

OK, so it seems the x-axis ticks on a log scale base 2 are off because the ticks align with powers of 10, even though the base is 2 and points aren't powers of 2.

Piecing together log scale ticks

On a log scale, tick labels represent multiplicative differences, which can cause uneven spacing. To achieve even spacing, consider using a linear scale or manually setting ticks at powers of 2.

Ensuring equal spacing

To achieve equal spacing between tick marks on a log scale, use base 2 in Matplotlib, ensuring the x-axis aligns properly with multiplicative differences.

Fine-tuning tick placement

To ensure equal spacing on a log base 2 scale in Matplotlib, use basex=2, set ticks at powers of 2, and adjust major and minor tick formatting accordingly.

Finished reasoning

الإجابة

对数坐标系 里,坐标轴上的距离不是按“数值差”来计算,而是按“倍数差”(对数值)来计算的。
所以一旦把 x 轴改成 plt.xscale('log'),刻度就会变成

物理距离 ∝ log_base(x)

造成“看起来不均匀”的两个常见原因

情况现象解决办法
仍在用 10 为底(默认)256、512、1024 … 不是 10 的整数次幂,Matplotlib 会自动在 10²~10³ 之间插若干“临时”主刻度,间距自然不等指定 2 为底<br>plt.xscale('log', base=2)(老版本写成 basex=2
已经是 base = 2,但想让每个数据点都当主刻度Matplotlib 只把 2⁰, 2¹, 2² … 当主刻度,其余点落在“次刻度”上,看起来位置会偏一点手动设主刻度:<br>```python
ax = plt.gca()
ax.set_xticks(point_counts) # 主刻度
ax.get_xaxis().set_major_formatter(
matplotlib.ticker.ScalarFormatter())  # 用普通数字显示

ax.set_xticklabels(point_counts) # 可选:完全自己写标签

text
### 一段完整示例 ```python import matplotlib.pyplot as plt import numpy as np import matplotlib.ticker as mticker point_counts = np.array([256, 512, 1024, 2048, 4096, 8192]) # … 你的 fft_single / fft_double / ifft_single / ifft_double … fig, ax = plt.subplots(figsize=(10, 6)) ax.plot(point_counts, fft_single, marker='o', label='FFT 单精度') ax.plot(point_counts, fft_double, marker='o', label='FFT 双精度') ax.plot(point_counts, ifft_single, marker='s', label='IFFT 单精度') ax.plot(point_counts, ifft_double, marker='s', label='IFFT 双精度') ax.set_xlabel('点数 N') ax.set_ylabel('耗时(ms)') ax.set_title('FFT/IFFT 耗时对比(单核)') # 关键:对数坐标 & 2 为底 ax.set_xscale('log', base=2) # 或 basex=2(旧版本) ax.set_yscale('log') # 让每个 N 都是主刻度 ax.set_xticks(point_counts) ax.get_xaxis().set_major_formatter(mticker.ScalarFormatter()) ax.set_xticklabels(point_counts) # 如果想保留原样标签 ax.grid(True, which='both', linestyle='--') ax.legend() plt.tight_layout() plt.show()

这样:

  • x 轴每翻一格都是“翻倍”,视觉间距相等;
  • 所有数据点都落在主刻度上,读数也更直观。

如果你希望 x 轴线性、刻度严格等距,只需把 ax.set_xscale('log', base=2) 去掉即可,但就失去了“对数刻度能把大范围数据压缩在同一张图里”的好处。

سؤال

0.003402s
0.002022s
0.003226s
0.003005s
0.004134s
0.007228s
0.013085s
0.024208s
0.047483s
0.093095s
0.240722s
0.618289s

0.00291s
0.003257s
0.004751s
0.010253s
0.006065s
0.011877s
0.026803s
0.04825s
0.078148s
0.125911s
0.439458s
1.217801s

0.006094s
0.002672s
0.003977s
0.003036s
0.004204s
0.010922s
0.01338s
0.025037s
0.04777s
0.09379s
0.238406s
0.629546s

0.002972s
0.003198s
0.003557s
0.008997s
0.007126s
0.015667s
0.026753s
0.051304s
0.08556s
0.128113s
0.442645s
1.219581s

这四个依次是fft_single,fft_double,ifft_single,ifft_double,请你转换为np.array的格式

الإجابة

当然可以,下面是这四组数据转换成 np.array 格式:

python
fft_single = np.array([0.003402, 0.002022, 0.003226, 0.003005, 0.004134, 0.007228, 0.013085, 0.024208, 0.047483, 0.093095, 0.240722, 0.618289]) fft_double = np.array([0.00291, 0.003257, 0.004751, 0.010253, 0.006065, 0.011877, 0.026803, 0.04825, 0.078148, 0.125911, 0.439458, 1.217801]) ifft_single = np.array([0.006094, 0.002672, 0.003977, 0.003036, 0.004204, 0.010922, 0.01338, 0.025037, 0.04777, 0.09379, 0.238406, 0.629546]) ifft_double = np.array([0.002972, 0.003198, 0.003557, 0.008997, 0.007126, 0.015667, 0.026753, 0.051304, 0.08556, 0.128113, 0.442645, 1.219581])

可以直接用在你前面的绘图代码中。需要我帮你把它们画出来看看效果吗?

مشاركة هذا السؤال والجواب