子图中的Python xticks [英] Python xticks in subplots
本文介绍了子图中的Python xticks的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我绘制单个imshow
图,我可以使用
If I plot a single imshow
plot I can use
fig, ax = plt.subplots()
ax.imshow(data)
plt.xticks( [4, 14, 24], [5, 15, 25] )
替换我的xtick标签.
to replace my xtick labels.
现在,我正在使用
f, axarr = plt.subplots(4, 3)
axarr[i, j].imshow(data)
如何仅为其中一个子图更改我的xticks?我只能使用axarr[i, j]
访问子图的轴.如何仅针对一个特定子图访问plt
?
How can I change my xticks just for one of these subplots? I can only access the axes of the subplots with axarr[i, j]
. How can I access plt
just for one particular subplot?
推荐答案
有两种方法:
- 使用子图对象的轴方法(例如
ax.set_xticks
和ax.set_xticklabels
)或 - 使用
plt.sca
设置pyplot状态机的当前轴(即plt
界面).
- Use the axes methods of the subplot object (e.g.
ax.set_xticks
andax.set_xticklabels
) or - Use
plt.sca
to set the current axes for the pyplot state machine (i.e. theplt
interface).
作为示例(这也说明了使用setp
来更改所有子图的属性):
As an example (this also illustrates using setp
to change the properties of all of the subplots):
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=3, ncols=4)
# Set the ticks and ticklabels for all axes
plt.setp(axes, xticks=[0.1, 0.5, 0.9], xticklabels=['a', 'b', 'c'],
yticks=[1, 2, 3])
# Use the pyplot interface to change just one subplot...
plt.sca(axes[1, 1])
plt.xticks(range(3), ['A', 'Big', 'Cat'], color='red')
fig.tight_layout()
plt.show()
这篇关于子图中的Python xticks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文