- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
查看 matplotlib
文档,似乎将 AxesSubplot
添加到 Figure
的标准方法是使用 Figure。 add_subplot
:
from matplotlib import pyplot
fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)
ax.hist( some params .... )
我希望能够独立于图形创建类似 AxesSubPlot
的对象,这样我就可以在不同的图形中使用它们。类似的东西
fig = pyplot.figure()
histoA = some_axes_subplot_maker.hist( some params ..... )
histoA = some_axes_subplot_maker.hist( some other params ..... )
# make one figure with both plots
fig.add_subaxes(histo1, 211)
fig.add_subaxes(histo1, 212)
fig2 = pyplot.figure()
# make a figure with the first plot only
fig2.add_subaxes(histo1, 111)
这在 matplotlib
中是否可行,如果可以,我该怎么做?
更新:我还没有设法解耦轴和图形的创建,但是按照下面答案中的示例,可以轻松地在新的或 olf Figure 实例中重新使用以前创建的轴。这可以用一个简单的函数来说明:
def plot_axes(ax, fig=None, geometry=(1,1,1)):
if fig is None:
fig = plt.figure()
if ax.get_geometry() != geometry :
ax.change_geometry(*geometry)
ax = fig.axes.append(ax)
return fig
最佳答案
通常,您只需将坐标区实例传递给函数。
例如:
import matplotlib.pyplot as plt
import numpy as np
def main():
x = np.linspace(0, 6 * np.pi, 100)
fig1, (ax1, ax2) = plt.subplots(nrows=2)
plot(x, np.sin(x), ax1)
plot(x, np.random.random(100), ax2)
fig2 = plt.figure()
plot(x, np.cos(x))
plt.show()
def plot(x, y, ax=None):
if ax is None:
ax = plt.gca()
line, = ax.plot(x, y, 'go')
ax.set_ylabel('Yabba dabba do!')
return line
if __name__ == '__main__':
main()
要回答您的问题,您总是可以这样做:
def subplot(data, fig=None, index=111):
if fig is None:
fig = plt.figure()
ax = fig.add_subplot(index)
ax.plot(data)
此外,您可以简单地将轴实例添加到另一个图形:
import matplotlib.pyplot as plt
fig1, ax = plt.subplots()
ax.plot(range(10))
fig2 = plt.figure()
fig2.axes.append(ax)
plt.show()
调整它的大小以匹配其他子图“形状”也是可能的,但它很快就会变得比它的值(value)更麻烦。根据我的经验,对于复杂情况,仅传递图形或轴实例(或实例列表)的方法要简单得多...
关于python - matplotlib:我可以创建 AxesSubplot 对象,然后将它们添加到 Figure 实例吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6309472/
你能帮我理解我做错了什么吗?我有 NetBeans、MinGW 编译器和 Qt 库。 *图.h* class Figure: public QObject{ //18 Q_OBJECT
一位程序员提醒我注意一个问题,matplotlib.pyplot 和 Tkinter 不能很好地协同工作,如这个问题所示 Tkinter/Matplotlib backend conflict cau
我试图在同一张图上绘制一系列函数。代码似乎运行正常,但是没有图片出来。只是简单的 如何修复? 最佳答案 在 IPython 控制台中,确保图形显示(无需显式调用 plt.show())的最佳方法是使
所以我将以下简单代码保存在 .py 文件中,并在 shell 中执行: import matplotlib.pyplot as plt myfig = plt.figure(figsize=(5, 5
我正在尝试处理带有对象的图形列表。不幸的是,从图表列表中绘图似乎有问题。 请注释掉下面示例中的行,您会看到绘图是如何中断的: import matplotlib as mpl from matplot
如 http://matplotlib.org/users/event_handling.html 中所述下面的示例代码工作正常 import matplotlib.pyplot as plt fig
我正在使用 matplotlib 绘图,并使用 ioff() 关闭交互模式,以禁止在创建图形时自动打开绘图窗口。我希望完全控制该图形,并且仅在显式使用 show() 命令时才能看到它。 现在显然用于清
创建matplotlib图形后是否可以保存它?例如: import matplotlib.pyplot as plt import numpy normal = numpy.random.normal
我开始构建 Tkinter 应用程序,最初使用的是 matplotlib 的 Figure 和 figure.add_subplot。有了它,一切都完美无缺。对于更多自定义,我现在想移动到 pyplo
我正在使用 Quarto 并渲染为 PDF。我有带有子图的数字。子图的标题很长(很多字)。问题是 PDF 中的子图标题没有分开。这是问题的说明; 请注意,子图的标题之间没有分隔(空白): 我尝试过使用
我正在阅读一些教程,包括 official docs似乎每个人都更喜欢输出一个图形。 例如: @app.callback( Output('graph-with-slider', 'figur
我正在阅读一些教程,包括 official docs似乎每个人都更喜欢输出一个图形。 例如: @app.callback( Output('graph-with-slider', 'figur
我正在尝试使用 Figure.transFigure.transform() 功能在图形坐标中绘制线条,但要么我不完全理解它,要么出现其他问题。根据Matplotlib Documentation ,
我有一个返回 Figure 对象的外部函数,在这种情况下,每个 Figure 对象都由一个 Axes 对象组成。 我想组成一个由这两个图形组成的新图形(比方说水平连接)。 所以理想情况下,我想说: f
我正在尝试制作一个包含六个独立地 block 的图形,这些地 block 分为两行,每行三个地 block 。每行图都应该有自己的颜色条,对应于水平组中三个图中显示的图像。从视觉上看,该图应如下所示:
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 9年前关闭。 Improve this q
matplotlib.figure.Figure.add_subplots() ( doc ) 应该返回一个轴。 但是,做 import matplotlib.pyplot as plt fig =
这是我的代码中出现错误的部分(都与图表相关,但要点: plt.figure (figsize = (10,6)) plt.title ("Alfa x CL") plt.plot (Alpha,CL,
我正在尝试使用破折号绘制一些数据的代码,我在想,我做对了。但不确定为什么会收到一条非常奇特的消息(plotly 3.8.1 and dash 0.42) 我得到的错误信息是: Invalid argu
为了去除图中的框架,我写了 frameon=False 与 pyplot.figure 完美搭配,但使用 matplotlib.Figure 只会移除灰色背景,框架保持不变。另外,我只希望线条显示出来
我是一名优秀的程序员,十分优秀!