matplotlib 获取给定图形包含的所有轴以应用一些设置

编程入门 行业动态 更新时间:2024-10-28 04:19:25
本文介绍了matplotlib 获取给定图形包含的所有轴以应用一些设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在编写一个修改图形上的轴尺寸和位置的函数,但是当出现双轴时会出现问题:

I'm writing a function that modifies the axes size and position on a figure, but when comes twin axes it makes a problem:

import matplotlib.pyplot as plt def fig_layout(fig, vspace = 0.3): # function to make space at the bottom for legend box and #+ other text input for ax in ~~~fig.axes~~~: # Here 'fig.axes' is not right, I need to find the exact syntax #+ I need to put box = ax.get_position() ax.set_position([box.x0, box.y0 + box.height * vspace, box.width, box.height * (1 - vspace)]) x = np.arange(10) fig = plt.figure() ax1 = fig.add_subplot(1, 1, 1) n = 3 line = {} for i in range(3): line['lines'].append(ax1.plot(x, i*x**2)) line['labels'].append(r'$y = %i \cdot x^2$'%i) ax1.set_title('example plot') ax2 = ax1.twinx() line['lines'].append(ax2.plot(x, x^-1, label = r'$y = x^-1$')) line['labels'].append(r'$y = x^-1$') leg = ax1.legend(line['lines'], line['labels']) fig_layout(fig) # I will put the legend box at the bottom of the axes with another function. plt.show()

推荐答案

我认为您可以使用fig.get_axes().

I think you can use fig.get_axes().

例如,要修改第一个子图的标题,您可以执行以下操作:

For example, to modify the title of the first sub-plot, you can do:

plt.gcf().get_axes()[0].set_title("example plot")

更多推荐

matplotlib 获取给定图形包含的所有轴以应用一些设置

本文发布于:2023-11-28 22:32:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644201.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图形   matplotlib

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!