python - 无法显示函数的图形
问题描述
给定一个函数 f(x),如何?
在具有共享 x 轴的一个底部和顶部子图的图形上生成函数及其导数函数的图形。
两个轴都采用输入数组的形式,输入数组的值从最小值到最大值,间隔为 0.5。每个子图必须有标题 顶部子图必须有 ylabel,而底部子图必须同时具有 xlabel 和 ylabel 每个子图镜头都有不同样式的线条(颜色、粗细等),x 刻度也必须在不同的样式(特别是字体大小和旋转)。
我这样做了:
#the function is f(x) while the derivative is df(x)
x = np.arange(min, max, 0.5) #scale
figure, (top, bottom) = plt.subplots(2, sharex=True, figsize = [5.0, 7.0])
top.plot(x, f(x), 'b', linewidth = 5)
top.set_title(Function f(x))
top.ylabel('f(x)')
figure.set_xticks(colors = 'r', fontsize = 12, rotation = 30)
bottom.plot(x, df(x), 'g-', linewidth = 8)
bottom.set_title('Derivative function of f(x)')
bottom.xlabel('x')
bottom.ylabel('df(x)')
plt.show(figure)
但它不起作用。我怎样才能解决所有问题?
为概括而编辑
解决方案
这似乎可以满足您的需要:
import numpy as np
import matplotlib.pyplot as plt
def f(x):
return np.sin(x) - x*np.cos(x) #The derivative is equal to x
x = np.arange(-5.0, 5.0, 0.05)
figure, (top, bottom) = plt.subplots(2, sharex=True, figsize = [5.0, 7.0])
top.plot(x, f(x), 'b', linewidth = 5)
top.set_title('Function f(x) = sin(x) - x*cos(x)')
top.set_ylabel('f(x)')
plt.tick_params(axis='x', colors = 'r', labelsize = 12, rotation = 30)
bottom.plot(x, x, 'g-', linewidth = 8)
bottom.set_title('Derivative function of f(x)')
bottom.set_xlabel('x')
bottom.set_ylabel('df(x)')
bottom.tick_params(axis='x', colors = 'r')
plt.show()
推荐阅读
- r - 在 ggplot2 中重新排序轴时,“eval 中的错误(substitute(expr),e):缺少参数,没有默认值”
- javascript - 如何修复附加字段,删除并在渲染字段上显示
- windows - 定时器回调函数出现意外的帧速率
- python - 如何在类中使用 if 语句
- rest - Salesforce SOQL 获取分配给登录用户的所有帐户
- python - 如何从 Python 中的 JSON 对象获取特定数据
- angular - 按天数计算的角管返回等级
- python - 如何使用接受命令行参数的嵌套模块对 python 脚本进行单元测试?
- reactjs - 从状态到 FieldArray 的数据
- javascript - 单个语句中的多个 if 条件