matplotlib - 在 Python 中绘制多个幂函数
问题描述
我正在尝试在 python 中绘制三个幂函数,但出现值错误。这是我的代码。我该如何解决?请帮忙
代码:
from numpy import *
import math
import matplotlib.pyplot as plt
a = logspace(-4, 2, 10)
m = 0.315*a**-3
r = 0.0000926*a**-2
d = 0.685
plt.plot(a, m, 'r')
plt.plot(a, r, 'b')
plt.plot(a, d, 'g')
plt.show()
错误信息:
ValueError: x and y must have same first dimension, but have shapes (10,) and (1,)
解决方案
正如 JohanC 建议的那样,您需要相同数量的元素a
和d
变量。如果有帮助,我只是使用他的建议并为您绘制图表。
import numpy as np
import matplotlib.pyplot as plt
a = np.logspace(-4, 2, 10)
m = 0.315*a**-3
r = 0.0000926*a**-2
d = 0.685
fig = plt.figure(figsize=(4, 3), dpi=200)
plt.plot(a, m, 'r', label='a v/s m')
plt.plot(a, r, 'b', label='a v/s r')
# plt.plot(a, d, 'g') # As JohanC suggested you need same number of elements for a and d variable
plt.plot(a, np.full_like(a, d), 'g', label='a v/s d') # alternatively use this
plt.yscale('log')
plt.grid()
plt.legend()
推荐阅读
- elixir - Elixir - 连接和匹配组合
- r - 按传递给函数的列名对数据框进行排序
- javascript - bundle.js 不使用 webpack 构建
- python - python将字符串(Mar18)转换为日期格式(201803)
- python - EOFError - 使用 ftplib 上传 .csv 文件
- javascript - AngularJs - 在表格中设置单选按钮值
- asp.net-mvc - 在类库中引用 MVC Core RemoteAttribute
- ruby-on-rails - 如何编写自定义验证以防止记录具有所有空属性?
- c# - 使用c#替换word文档的标题
- php - 找到php内存泄漏的最佳方法?