如何通过指定行和列从轴对象返回特定轴?

如何解决如何通过指定行和列从轴对象返回特定轴?

我在 Jupyter Notebook 上学习 Python 已经有一段时间了,因为我的导师会布置作业。本周我需要实现一个函数,它执行以下操作:

实现函数 selected_axis 以便使用函数 plot_subplots 绘制以下图。 查看 plot_subplots 函数以了解您的函数接收什么作为参数以及预期返回值是什么。

我已经加载了以下库:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np 

这里我得到了 plot_subplots 函数:

def plot_subplots(data,nb_rows,nb_cols,row,col):

    fig,axes = plt.subplots(nb_rows,sharex=True,sharey=True)

    axis = selected_axis(axes,col,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,1,2)

注释显示函数如何工作,以及它接收哪些变量。所以我继续实现我的函数selected_axis

def selected_axis(axes,nb_cols): 
    a = []
    if row <= nb_rows and col <= nb_cols:
        for column_id,rows in enumerate(axes):
            if column_id == col - 1:
                a.append(col-1)
        for row_id,row_value in enumerate(rows):
            if row_id == row - 1:
                a.append(row-1)

    axis = axes[a[1],a[0]] 
    return axis

如果我运行 plot_subplots(np.arange(1,2),则会给出以下输出,这似乎与我被要求实现的目标一致,即在所需的子图中绘图:

Output of plot_subplots(np.arange(1,2)

现在,我面临着我的导师实施的以下测试单元:

# Test cell.
from unittest import TestCase

__ = TestCase()

_data,_rows,_cols,_row,_col = np.arange(1,2
_fig,_axes = plt.subplots(_rows,sharey=True)
_ax = selected_axis(_axes,_col,_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,2,5,4
_fig,msg="Your function does not select the correct axis.")
_ax.plot(_data)
plt.show()

_fig,_axes = plt.subplots(2,sharey=True)
selected_axis(_axes,2).plot(np.arange(-5,0.1) ** 2)
selected_axis(_axes,2).plot(-np.arange(-5,0.1) ** 3)
selected_axis(_axes,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,_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,_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.

截至目前,我完全不知道为什么测试单元没有给我预期的结果..

如果有人能帮助我,我将不胜感激!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-