我试着画出这个算法的一条线或散点图,它给了我错误Traceback (most recent call last):

File "/Users/itstest/Documents/workspace/Practice/src/PlutoModel.py", line 73, in module

plt.plot(xr, P(xr))

File "/Users/itstest/Documents/workspace/Practice/src/PlutoModel.py", line 55, in P

if x > r:

ValueError: "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()."

我已经找到了解决这个错误的可能办法,但我不认为有什么适合我的。在import numpy as np

import scipy.integrate as integ

import matplotlib.pyplot as plt

rho = 1860

rhos = 250 #Assuming Nitrogen Ice

rhom = 1000 #Assuming Water

rhoc = 3500 #Assuming a mix of Olivine and Pyroxene

def rhof(x):

if x > r:

return "Point is outside of the planet"

elif x < r and x > rm:

return rhos

elif x < rm and x > rc:

return rhom

else:

return rhoc

r = 1.187e6

rc = 8.5e5 #Hypothesized

rm = 9.37e5 #Estimated based on crustal thickness of 250 km

Ts = 44

B = 0.15

G = 6.67e-11

m = 1.303e22

mc = (4*np.pi*rhoc*rc**3)/3

mm = (4*np.pi*rhom*((rm**3) - (rc**3)))/3

ms = (4*np.pi*rhos*((r**3) - (rm**3)))/3

Ic = 0.4*mc*rc**2

Im = 0.4*mm*rm**2

Is = 0.4*ms*r**2

Itot = Is + Im + Ic

def gi(x):

if x == r:

return G*m/r**2

elif x > r:

return "Point is outside of the planet"

elif x > rc and x < rm:

return (G*mc/rc**2) + (G*mm/((x**2) - (rc**2)))

elif x > rm and x < r:

return (G*mc/rc**2) + (G*mm/((rm**2) - (rc**2))) + (G*ms/((x**2) - (rm**2)))

else:

return G*((3*rhoc)/4*np.pi*x**3)/x**2

def Psmb(z):

return rhos*G*(4.0/3.0)*np.pi*(1/z**2)*(rhom*(rm**3) + rhos*(z - rm**3))

def Pmcb(z):

return rhom*G*(4.0/3.0)*np.pi*(1/z**2)*(rhoc*(rc**3) + rhom*(z - rc**3))

def P(x):

if x > r:

return "The point is outside of the planet"

elif x == r:

return 1

elif x > rm and x < r:

return (integ.quad(1000*gi(x), x, r))[0]

elif x == rm:

return (integ.quad(Psmb, x, r))[0]

elif x > rc and x < rm:

return (integ.quad(1000*gi(x), x, rm) + P(rm))[0]

elif x == rc:

return (integ.quad(Pmcb, x, rm) + P(rm))[0]

elif x < rc and x != 0:

return (integ.quad(1000*gi(x), x, rc) + P(rc))[0]

else:

return ((2.0/3.0)*np.pi*G*(rhoc**2)*r**2)

xr = np.linspace(0, 1187000, 1000)

plt.plot(xr, P(xr))

print("Mass = " + str(m) + " kg")

print("Radius = " + str(r) + " m")

print("Density = " + str(rho) + " kg/m^3")

print("Moment of Inertia = " + str(Itot) + " kgm^2")

print("Mean Surface Temperature = " + str(Ts) + " K")

print("Magnetic Field = " + str(B) + " nT")

print("Surface Gravity = " + str(gi(r)) + " m/sec^2")

print("Pressure at Surface = " + str(P(r)) + " Pa")

print("Pressure at Crust-Mantle Boundary = " + str(P(rm)) + " Pa")

print("Pressure at Mantle-Core Boundary = " + str(P(rc)) + " Pa")

print("Pressure at the Center = " + str(P(0)) + " Pa")

有没有办法在不分离每个条件的情况下绘制这个函数?在

python的matplotlib的函数_在Python中使用matplotlib绘制条件函数相关推荐

  1. python中transpose函数_对numpy中的transpose和swapaxes函数详解

    transpose() 这个函数如果括号内不带参数,就相当于转置,和.T效果一样,而今天主要来讲解其带参数. 我们看如下一个numpy的数组: `arr=np.arange(16).reshape(( ...

  2. java里的进制转换函数_基于Java中进制的转换函数详解

    十进制转成十六进制: Integer.toHexString(int i) 十进制转成八进制 Integer.toOctalString(int i) 十进制转成二进制 Integer.toBinar ...

  3. python中fill函数_在figu中旋转matplotlib的fill函数

    我在试着做一个三人合谋.其中一个绘图框相对于另一个旋转90度,并垂直于另一个绘图的轴.所以我可以在这个帧中绘制一个直方图图,但是当我使用kde生成数据并使用fill覆盖到{}时,它不会旋转.在impo ...

  4. python tk 获取鼠标事件_在Tkin中列出鼠标悬停事件函数

    我正在制作一个医学工具的图形用户界面作为一个类项目.给定一个条件,它应该输出一堆从不同网站收集的治疗方案,比如webMD.我希望能够处理所列出的任何一种疗法的鼠标悬停事件,以提供关于该疗法的更多信息( ...

  5. matlab中rastrigin图形绘制,matlab函数function

    x?6 x?6 (1) 利用MATLAB语言编写S函数.程序如下: function [sys,x0,str,ts]=sfunction(t,x,u,flag) switch flag, case 0 ...

  6. python清掉内存的函数_关于python:如何彻底清除所有Matplotlib图的内存

    我有一个数据分析模块,该模块包含多次调用Matplotlib pyplot API的函数,每次运行最多生成30个图形. 这些数字生成后立即被写入磁盘,因此我需要从内存中清除它们. 目前,在每个功能结束 ...

  7. python画三维折线图_使用Python的matplotlib画折线图,柱状图,三维图

    因为在各种场景下需要各种实验数据的对比图像,有的中还要求dpi,这些在Python中的matplotlib中都可以实现,下面是总结的各种画图命令. 打包文件:https://download.csdn ...

  8. python plot方法的使用_【python】matplotlib.pyplot入门

    matplotlib.pyplot介绍 matplotlib的pyplot子库提供了和matlab类似的绘图API,方便用户快速绘制2D图表. matplotlib.pyplot是命令行式函数的集合, ...

  9. python 二维强度图_荐 python数据分析matplotlib库使用之二维图形绘制

    本篇内容会在后期不定时更新 什么是matplotlib matplotlib是最流行的python底层绘图库,主要做数据可视化图表. 为什么要学习matplotlib 能将数据进行可视化,更直观的呈现 ...

最新文章

  1. java 自定义异常 未回滚_抛出自定义异常,spring AOP事务不回滚的解决方案
  2. C++ class、struct区别
  3. 为什么C# md5 32位加密算法,密码明文会出现31位
  4. dbf文件怎么创建_DBC文件到底是个啥
  5. 积累分布用例-洛伦兹曲线和随机游走
  6. 循迹避障小车制作第一篇(tb6612模块与降压模块的使用)
  7. html 图片放大保证不失真,图片放大不失真的几种方法
  8. java转换docx为doc文件_JAVA - 将doc文档转为docx文档
  9. EditPlus 使用 json 格式化
  10. DataFrame的ix方法
  11. 吐槽大会 之博客园两个月使用体验
  12. 名帖52 王献之 小楷《洛神赋》
  13. 斐讯路由器虚拟服务器怎么设置,斐讯PSG1218路由器如何设置
  14. 4_kicad 5.0_spice仿真器(AC,直流切换,失真,噪声,操作点,临界点,灵敏度,传递函数,瞬态,自定义)...
  15. 2020移动apn接入点哪个快_移动水上乐园里的充气水池和支架水池,哪个回本快?...
  16. 7-148 试试多线程 (10 分)
  17. 给女友的网页小惊喜,(生日,周年,表白通用) ☞谁说程序员不懂浪漫
  18. 万达商业被曝暂停港股IPO:曾拟募资30亿美元 王健林梦难圆
  19. Webug 4.0下载地址
  20. 不懂SpringApplication生命周期事件?那就等于不会Spring Boot嘛

热门文章

  1. 编译环境与生成环境的JDK版本不一样,报:java.util.zip.ZipException: error in opening zip file
  2. Spring框架学习笔记09:基于XML配置方式搭建SSM框架实现用户登录
  3. 《天天数学》连载19:一月十九日
  4. Python学习笔记:函数(Function)
  5. Must 和 have to_54
  6. layui表头样式_js相关:layui中table表头样式修改方法
  7. 南京江南贡院值得去吗_江南贡院,去南京的必游之地!
  8. 【英语学习】【加州教材】【G5】【科学】Science目录及术语表
  9. 全方面的了解超宽带信号高速采集记录回放系统
  10. 移动开发-语音识别-调用讯飞平台提供的API