python - 为 matplotlib 提供 Y 尺度

标签 python matplotlib

我有两个轴。我想在 ax1 中绘制 4e-6 并在 ax2 中绘制 4e-7 。如何使 ax1 的 y 轴像 ax2 的 y 轴一样?即,它应该缩放到 1e-6 并且比例应该出现在 y 轴的顶部。

这是代码:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(10,5))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
x = np.linspace(0, 1000, 3)
y1 = np.full_like(x, 4e-6)
y2 = np.full_like(x, 4e-7)

ax1.plot(x, y1)
ax2.plot(x, y2)
plt.show()

这是图:

enter image description here

最佳答案

这可以如下所示实现:default_tick_formatter

将 y_axis 值指定为 4*1e-7 而不是 4e-7,将得到满足您要求的解决方案。

重新创建了以下内容并生成了以下解决方案:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(10,5))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
x = np.linspace(0, 1000, 3)
y1 = np.full_like(x, 4*1e-6)
y2 = np.full_like(x, 4*1e-7)

ax1.plot(x, y1)
ax2.plot(x, y2)
plt.show()

结果看起来是 result

关于python - 为 matplotlib 提供 Y 尺度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69164797/

相关文章:

python - 箱线图按 python pandas 中的列分层

python - 根据python中的索引列表获取列表中的元素

python - “numpy.ndarray”对象不可调用文件未正确选择

python - 添加一个新的 pandas 列,其中包含字典中的映射值

python - celery block 内链

python - 我正在绘制 RLC 电路的输出电压,但不知道如何输入公式

python - matplotlib 共享 x 轴,但不显示两者的 x 轴刻度标签,只有一个

python - 如何计算包含一组列中的值和 Pandas 数据框中另一列中的另一个值的行数?

python - 用 matplotlib 表示体素

python - pylab散点图出现转置