Python 如何通过指定行和列从axis对象返回特定轴?
我在Jupyter笔记本上学习Python已经有一段时间了,我的导师给我布置了作业。本周,我需要实现一个函数,该函数执行以下操作: 执行功能选择_轴,以便使用功能图_子图绘制以下图。 查看plot_subplot函数,了解函数作为参数接收的内容以及预期的返回值 我已加载以下库:Python 如何通过指定行和列从axis对象返回特定轴?,python,numpy,matplotlib,figure,axes,Python,Numpy,Matplotlib,Figure,Axes,我在Jupyter笔记本上学习Python已经有一段时间了,我的导师给我布置了作业。本周,我需要实现一个函数,该函数执行以下操作: 执行功能选择_轴,以便使用功能图_子图绘制以下图。 查看plot_subplot函数,了解函数作为参数接收的内容以及预期的返回值 我已加载以下库: import pandas as pd import matplotlib.pyplot as plt import numpy as np 这里我得到了plot_子图函数: def plot_subplots(da
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
这里我得到了plot_子图函数:
def plot_subplots(data, nb_rows, nb_cols, row, col):
fig, axes = plt.subplots(nb_rows, nb_cols, sharex=True, sharey=True)
axis = selected_axis(axes, row, col, nb_rows, nb_cols)
axis.plot(data)
plt.show()
# Plot a linear function in a 3x3 matrix at position 1,2
plot_subplots(np.arange(1, 9), 3, 3, 1, 2)
注释显示函数将如何工作,以及它接收哪些变量。因此,我继续并实现了我的函数选择_轴:
执行时,我收到以下错误消息:
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-129-fb042c0cacf0> in <module>
8 _ax = selected_axis(_axes, _row, _col, _rows, _cols)
9
---> 10 __.assertTrue(_ax.get_subplotspec().colspan.start == _col and _ax.get_subplotspec().rowspan.start == _row,
11 msg="Your function does not select the correct axis.")
12 _ax.plot(_data)
~\anaconda3\lib\unittest\case.py in assertTrue(self, expr, msg)
763 if not expr:
764 msg = self._formatMessage(msg, "%s is not true" % safe_repr(expr))
--> 765 raise self.failureException(msg)
766
767 def _formatMessage(self, msg, standardMsg):
AssertionError: False is not true : Your function does not select the correct axis.
到目前为止,我完全不知道为什么测试单元没有给我预期的结果
如果有人能帮助我,我将不胜感激 这个错误来自测试用例,而不是python,它说您在错误的子图中绘制了。您的输出在第0行第1列中显示了一行,但这些指令似乎要求您在第1行第2列中绘制,假设这些指令(如python)为0-indexed@G.Anderson谢谢你的回复!我在every.append语句中取出了-1,现在我遇到了indexer错误:列表索引超出-->17\u ax=选定的轴、行、列、列的范围,一旦我对该错误有了新的了解,我将立即更新!最好只是用axes=axes.flatte展平轴,这样你就有了可以索引的平面数组,这要容易得多。@TrentonMcKinney谢谢!我会试试看,看看我能做些什么!更新:让它工作,基本上我可以得到一个解决我的代码,我没有添加任何新的,只是重申它!问题实际上是混淆了for循环中的列和行。我需要了解Axis对象到底是什么,这确实帮助我理解了我的问题。谢谢@G.Anderson和@TrentonMcKinney的帮助!非常感谢!
# Test cell.
from unittest import TestCase
__ = TestCase()
_data, _rows, _cols, _row, _col = np.arange(1, 9), 3, 3, 1, 2
_fig, _axes = plt.subplots(_rows, _cols, sharex=True, sharey=True)
_ax = selected_axis(_axes, _row, _col, _rows, _cols)
__.assertTrue(_ax.get_subplotspec().colspan.start == _col and _ax.get_subplotspec().rowspan.start == _row,
msg="Your function does not select the correct axis.")
_ax.plot(_data)
plt.show()
_data, _rows, _cols, _row, _col = np.arange(1, 9), 2, 5, 1, 4
_fig, _axes = plt.subplots(_rows, _cols, sharex=True, sharey=True)
_ax = selected_axis(_axes, _row, _col, _rows, _cols)
__.assertTrue(_ax.get_subplotspec().colspan.start == _col and _ax.get_subplotspec().rowspan.start == _row,
msg="Your function does not select the correct axis.")
_ax.plot(_data)
plt.show()
_fig, _axes = plt.subplots(2, 2, sharex=True, sharey=True)
selected_axis(_axes, 0, 0, 2, 2).plot(np.arange(-5, 5, 0.1) ** 2)
selected_axis(_axes, 0, 1, 2, 2).plot(-np.arange(-5, 5, 0.1) ** 2)
selected_axis(_axes, 1, 0, 2, 2).plot(np.arange(-5, 5, 0.1) ** 3)
selected_axis(_axes, 1, 1, 2, 2).plot(-np.arange(-5, 5, 0.1) ** 3)
__.assertTrue(_ax.get_subplotspec().colspan.start == _col and _ax.get_subplotspec().rowspan.start == _row,
msg="Your function does not select the correct axis.")
_ax.plot(_data)
plt.show()
__.assertRaises(ValueError, selected_axis, _axes, 1000, 312, _rows, _cols)
print("\n\033[37;42;2m Success! Your code works as intended. \033[0m\n")
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-129-fb042c0cacf0> in <module>
8 _ax = selected_axis(_axes, _row, _col, _rows, _cols)
9
---> 10 __.assertTrue(_ax.get_subplotspec().colspan.start == _col and _ax.get_subplotspec().rowspan.start == _row,
11 msg="Your function does not select the correct axis.")
12 _ax.plot(_data)
~\anaconda3\lib\unittest\case.py in assertTrue(self, expr, msg)
763 if not expr:
764 msg = self._formatMessage(msg, "%s is not true" % safe_repr(expr))
--> 765 raise self.failureException(msg)
766
767 def _formatMessage(self, msg, standardMsg):
AssertionError: False is not true : Your function does not select the correct axis.