gpt4 book ai didi

python - 带限制的 Matplotlib 对数刻度关闭底部/向上绘图脊柱

转载 作者:行者123 更新时间:2023-12-01 05:19:49 25 4
gpt4 key购买 nike

我正在尝试生成一个对数(以 2 为底)图,但我不断得到一个没有顶部/底部边框的图。

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

def toK (array):
return map (lambda x: x/1000.0, array)


yy = [2603.76, 41077.89,48961.74, 43471.14]
xx = [1,16,32,64]

ax = plt.subplot(221, axisbg = 'white')
ax.set_xlim(0, 128)


ax.set_xscale('log', basex=2)



ax.plot( xx, toK(yy), label="0%", linestyle='--', marker='o', clip_on = False)

plt.savefig('./tx2.pdf', bbox_inches='tight')

Unboder plit

我怎样才能正确地做到这一点?

最佳答案

那是因为使用对数刻度时将 0 作为限制。 (0 在对数刻度上处于负无穷大)

将轴限制设置为包含零可能会引发错误,但目前,它只是默默地导致一些事情被破坏。

如果您希望绘图上有 0,请使用 symlog 而不是 log。然而,在这种情况下,将最小值设置为 2^-1(即 0.5)可能更有意义。

例如,可以这样做:

import matplotlib.pyplot as plt
import numpy as np

yy = np.array([2603.76, 41077.89,48961.74, 43471.14])
xx = [1,16,32,64]

fig, ax = plt.subplots()
ax.set_xlim(0.5, 128)

ax.set_xscale('log', basex=2)

ax.plot(xx, yy / 1000, linestyle='--', marker='o', clip_on=False)
plt.show()

enter image description here

或者使用“symlog”代替对数刻度:

import matplotlib.pyplot as plt
import numpy as np

yy = np.array([2603.76, 41077.89,48961.74, 43471.14])
xx = [1,16,32,64]

fig, ax = plt.subplots()
ax.set_xlim(0, 128)

ax.set_xscale('symlog', basex=2)

ax.plot(xx, yy / 1000, linestyle='--', marker='o', clip_on=False)
plt.show()

enter image description here

关于python - 带限制的 Matplotlib 对数刻度关闭底部/向上绘图脊柱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22617237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com