无法删除y轴上的前导零

2024-06-28 20:17:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在Matplotlib中绘制图形,并将它们嵌入pyqt5gui。一切都很正常,除了y轴有很多前导零,我似乎无法摆脱。你知道吗

我试过谷歌如何格式化轴,但似乎没有任何工作!我不能直接设置刻度,因为无法确定它们将是什么,因为我将使用不同大小的数据集。你知道吗

num_bins = 50

# create an axis
ax = self.figure.add_subplot(111)

# discards the old graph
ax.clear()

##draws the bars and legend
colours = ['blue','red']
ax.hist(self.histoSets, num_bins, density=True, histtype='bar', color=colours, label=colours)
ax.legend(prop={'size': 10})


##set x ticks
min,max = self.getMinMax()
scaleMax = math.ceil((max/10000))*10000
scaleMin = math.floor((min/10000))*10000
scaleRange = scaleMax - scaleMin
ax.xaxis.set_ticks(np.arange(scaleMin, scaleMax+1, scaleRange/4))


# refresh canvas
self.draw()

enter image description here


Tags: theselfmathaxminnummaxlegend
1条回答
网友
1楼 · 发布于 2024-06-28 20:17:16

y轴上的所有数字都很小,即1e-5的数量级。这是因为密度的积分被定义为1,而你的x轴跨越了如此大的范围

我基本上可以用以下方法重现你的情节:

import matplotlib.pyplot as plt
import numpy as np

y = np.random.normal([190000, 220000], 20000, (5000, 2))

a, b, c = plt.hist(y, 40, density=True)

给我:

above plot

^{}返回的元组包含有用的信息,特别是第一个元素(上面的a)是密度,第二个元素(上面的b)是它选择的容器。通过执行以下操作,您可以将所有的总和都转换为一:

sum(a[0] * np.diff(b))

把1拿回来。你知道吗

重要的是,fbeingernest说,如果绘图不适合该区域,可以使用^{}调整其大小

相关问题 更多 >