如何改变x轴在对数尺度上由大变小?

2024-04-24 11:07:39 发布

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

我有一个图,其中x和y值的起点应该是100。x轴需要对数标度。你知道吗

我不能反转我的x刻度从100开始到0。你知道吗

我的代码:

ax = df.plot(y ='ais_percent', x = 'rtcm_percent', color = 'firebrick')
ax.set_xscale("log")
ax.set_yscale("linear")

ax.invert_yaxis()
ax.invert_xaxis()
ax.set_xlim(1e2,0)

这是我的故事情节:

enter image description here y刻度正确,但x轴应相反。你知道吗

我该怎么做?你知道吗


Tags: 代码dfplot对数axcolor起点percent
2条回答

未定义日志(0)。改变

ax.set_xlim(1e2,0)例如到ax.set_xlim(1e2, 1e-300)

参考反转x轴,只需设置xlim即可反转。所以ax.invert_xaxis()是没有必要的。你知道吗

例如,请参见以下代码段:

import pandas as pd
import matplotlib.pyplot as plt

squareroots = [(i, i**0.5) for i in range(0, 100)]
squareroot_df = pd.DataFrame(squareroots, columns=['i', 'i^0.5'])

ax = squareroot_df.plot(x='i', y='i^0.5', color='firebrick')

ax.set_xscale('log')
# ax.invert_xaxis()  # not necessary
ax.set_xlim(1e2, 1e-50)

plt.show()

设置限制(使用方法set_xlim())重置invert_xaxis()。所以你必须改变命令的顺序:

而不是

ax.invert_xaxis()
ax.set_xlim(1e2,0)

使用

ax.set_xlim(1e2,0)
ax.invert_xaxis()

相关问题 更多 >