python - 如何在每个子图中设置轴限制

标签 python matplotlib

我用这段代码创建了一个子图:

f, axs =plt.subplots(2,3)

现在在一个循环中,我通过执行以下操作在每个子图上绘制一个图:

for i in range(5):
 plt.gcf().get_axes()[i].plot(x,u)

是否有类似的代码来设置我正在访问的子图的轴限制?

最佳答案

是的,有,但让​​我们清理一下代码:

f, axs = plt.subplots(2, 3)

for i in range(5): #are you sure you don't mean 2x3=6?
    axs.flat[i].plot(x, u)
    axs.flat[i].set_xlim(xmin, xmax)
    axs.flat[i].set_ylim(ymin, ymax) 

使用 axs.flat 将您的 axs (2,3) 轴数组转换为长度为 6 的平面可迭代轴。比 更容易使用plt.gcf().get_axes().

如果您只使用 range 语句来遍历轴并且从不使用索引 i,则只需遍历 axs

f, axs = plt.subplots(2, 3)

for ax in axs.flat: #this will iterate over all 6 axes
    ax.plot(x, u)
    ax.set_xlim(xmin, xmax)
    ax.set_ylim(ymin, ymax) 

关于python - 如何在每个子图中设置轴限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30261054/

相关文章:

python - 如何使用 matplotlib 读取 32 位整数图像?

python - 完全删除 matplotlib 中曲面图上的网格?

python - 如何配置 ZeroRPC 和超时

python - 提高for循环效率

python - 在个人计算机上加载 PipelineModel 时出现 ValueError

Python matplotlib 顺时针饼图

python - Matplotlib xticks 标签 : how to modify attributes value

python - 是否有适当的方法将复合希腊字母设置为 SymPy 中的符号?

python - numpy数组的扁平化

python - 组合 pandas 绘图时 ylim 的问题