子图中的Python xticks [英] Python xticks in subplots

查看:779
本文介绍了子图中的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?

推荐答案

有两种方法:

  1. 使用子图对象的轴方法(例如ax.set_xticksax.set_xticklabels)或
  2. 使用plt.sca设置pyplot状态机的当前轴(即plt界面).
  1. Use the axes methods of the subplot object (e.g. ax.set_xticks and ax.set_xticklabels) or
  2. Use plt.sca to set the current axes for the pyplot state machine (i.e. the plt 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆