缩放/显示Y轴Matplotlib

时间:2020-10-03 23:56:41

标签: python matplotlib

我对Python /数据集很陌生。我不确定在描述我的问题时使用的术语。结果,很难找到答案。我已为y轴绘制了一个范围为0-20,000,000,000,000的数据集。这是我所拥有的:

enter image description here

但是,对于我的任务,它应该像这样:

enter image description here

我尝试过ticklabel_format(style ='plain',axis ='y')这可以给我:

enter image description here

这给了我没有科学计数法和太多零的数字!我不确定应该使用哪种方法来缩放数字。我曾考虑过要对原始数据值进行操作,但这似乎并不正确。我查看了matplotlib.pyplot.yscale的文档,但除非我缺少某些内容,否则似乎并不是我所需要的。任何帮助表示赞赏。谢谢。

2 个答案:

答案 0 :(得分:0)

要将Y轴更改为指数轴,可以使用以下设置。请参阅此page

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import ScalarFormatter

x = np.arange(0, 200000000, 1)
fig, ax = plt.subplots(figsize=(6, 6))

ax.plot(x * 1000000, x * 100000)
ax.yaxis.set_major_formatter(ScalarFormatter(useMathText=True))
ax.ticklabel_format(style="sci",  axis="y", scilimits=(0,0))

plt.show()

enter image description here

答案 1 :(得分:0)

您可以使用numpy.array缩放数据,然后在y轴上指定缩放比例:

import numpy as np
import matplotlib.pyplot as plt 

#scale the data appropriately with numpy.array
dataset_1 = np.array(dataset_1)
scaled_dataset_1 = dataset_1/(10**9)

#plot with scaling specified in label
plt.plot(x_axis, scaled_dataset_1)
plt.ylabel('Dataset 1 (10^9)')
相关问题