AxesSubplot 对象的 xticks 的等效函数

发布于 2024-12-19 19:13:10 字数 982 浏览 2 评论 0原文

所以我尝试使用 Axes 对象来控制我的 matlibplot 图。我没有使用 plt (又名 import matlibplot.pyplot as plt),因为我将图形嵌入到我的 tkinter GUI 中 这个

但是,我也在图中使用了子图,所以类似于:

a = f.add_subplot(121)
a2 = f.add_subplot(122)
a.plot(fn2,mag)
a2.bar(range(0,10), magBin, width)

这一切都很好,我可以使用坐标区属性来控制事物(即 a.axesMethod()),但我想要我的条形图的字符串标签,根据这个,请参阅代码

我的困境是我不能使用

plt.xticks(ind+width, ('G1', 'G2', 'G3', 'G4', 'G5') )

示例中的 As,因为如果我想将它嵌入到我的 Tkinter GUI 中,我就不能使用 plt。我只能使用 Axes 对象做一些事情。我正在尝试使用 a2.set_xticks,但这不允许将字符串用作条形图所需的刻度功能。

So I am trying to use Axes objects to control my matlibplot figure. I am not using plt (aka import matlibplot.pyplot as plt) because I am embedding the figure in my tkinter GUI per this.

However, I am also using subplots in the figure, so something like:

a = f.add_subplot(121)
a2 = f.add_subplot(122)
a.plot(fn2,mag)
a2.bar(range(0,10), magBin, width)

This is all well and good, I can use the axes properties to control things (i.e. a.axesMethod()), but I want string labels for my bar plots, per this, see code.

My dilemma is that I cannot use

plt.xticks(ind+width, ('G1', 'G2', 'G3', 'G4', 'G5') )

As in the example, because I cannot use plt if I want to embed it into my Tkinter GUI. I am limited to what I can do with Axes objects. I am trying to use a2.set_xticks, but this does not allow for the string as ticks functionality I need for my bar chart.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枯叶蝶 2024-12-26 19:13:10

你可以使用:

axes.set_xticks(ticks, minor=False)

axes.set_xticklabels(labels, fontdict=None, minor=False)

you can use instead:

axes.set_xticks(ticks, minor=False)

and

axes.set_xticklabels(labels, fontdict=None, minor=False)
诗酒趁年少 2024-12-26 19:13:10

从 matplotlib 3.5.0 开始,您可以将标签作为第二个参数传递给 ax.set_xticks()(第一个参数是刻度位置),就像使用 plt.xticks( )。因此,一个有效的示例是:

f, ax = plt.subplots()
x = [10, 20, 30, 40, 50]
y = [1, 2.8, 3, 3.1, 5]
ax.plot(x, y)
ax.set_xticks(x, ['a', 'b', 'c', 'd', 'e'])

您也可以使用 set()

ax.set(xticks=ticks, xticklabels=labels)

需要注意两件事:(a) len(ticks)==len(labels) 应该是正确,并且 (b) 要在整个轴上充分分布 xticks,请使用 ax.get_xlim() 获取 x 轴刻度的限制并创建从以下位置开始和结束的值范围:限制。因此,一个有效的示例可能是:

f, ax = plt.subplots()
ax.plot(x, y)
ax.get_xlim()  # (8.0, 52.0)
ax.set(xticks=range(8, 53, 10), xticklabels=['a', 'b', 'c', 'd', 'e'], yticks=range(1, 6), yticklabels=range(1, 6));

res

Since matplotlib 3.5.0, you can pass labels as the second argument to ax.set_xticks() (the first argument is the tick locations), the same way you would with plt.xticks(). So a working example would be:

f, ax = plt.subplots()
x = [10, 20, 30, 40, 50]
y = [1, 2.8, 3, 3.1, 5]
ax.plot(x, y)
ax.set_xticks(x, ['a', 'b', 'c', 'd', 'e'])

You can use set() as well:

ax.set(xticks=ticks, xticklabels=labels)

Two things to note: (a) len(ticks)==len(labels) should be True, and (b) to adequately spread out the xticks across the whole axis, use ax.get_xlim() to get the limits of the x-axis ticks and create a range of values starting and ending at the limits. So a working example could be:

f, ax = plt.subplots()
ax.plot(x, y)
ax.get_xlim()  # (8.0, 52.0)
ax.set(xticks=range(8, 53, 10), xticklabels=['a', 'b', 'c', 'd', 'e'], yticks=range(1, 6), yticklabels=range(1, 6));

res

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文