gpt4 book ai didi

python - 将 x 轴刻度更改为自定义字符串

转载 作者:行者123 更新时间:2023-12-04 22:42:12 24 4
gpt4 key购买 nike

我想将 x 轴刻度标签更改为自定义字符串。

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import matplotlib.pyplot as plt

def pushButtonClicked(self):
code = self.lineEdit.text()


x=["one","two","three"]
l=[1,2,3]
y=[2,3,4]
ax = self.fig.add_subplot(111)

print(1)

ax.plot(l, y, label='DeadPopulation')
ax.xticks(l,x)
print(IntroUI.g_sortArrayDeadcnt)

ax.legend(loc='upper right')
ax.grid()
self.canvas.draw()

尽管搜索了许多站点并找到了许多代码示例,但我无法解决此问题。我的代码有什么问题?

最佳答案

我假设您只想将刻度设置为等于 ['one', 'two', 'three'] ?

为此,您需要使用 set_xticks()set_xticklabels() :

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import matplotlib.pyplot as plt

def pushButtonClicked(self):
code = self.lineEdit.text()


x=["one","two","three"]
l=[1,2,3]
y=[2,3,4]
ax = self.fig.add_subplot(111)

print(1)

ax.plot(l, y, label='DeadPopulation')

# Set the tick positions
ax.set_xticks(l)
# Set the tick labels
ax.set_xticklabels(x)

print(IntroUI.g_sortArrayDeadcnt)

ax.legend(loc='upper right')
ax.grid()
self.canvas.draw()

最小的例子
import matplotlib.pyplot as plt
f, ax = plt.subplots()

x = ['one', 'two', 'three']
l = [1, 2, 3]
y = [2, 3, 4]

ax.plot(l,y)
ax.set_xticks(l)
ax.set_xticklabels(x)

plt.show()

这是它的样子:

Plot with custom x-axis ticklabels

关于python - 将 x 轴刻度更改为自定义字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43673884/

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