我的代码


import matplotlib.pyplot as plt
import numpy as np

xmin, xmax=-3,3
x= np.array(range(xmin, xmax+1))
y,ysqr,ycube = x,x**2,x**3

f, (ax1, ax2, ax3) = plt.subplots(1,3, figsize=(25,8))

ax1.plot(x, y)
ax1.set_title('y=x')

ax2.plot(x, ysqr)
ax2.set_title('y=x^2')

ax3.plot(x, ycube)
ax3.set_title('y=x^3')

plt.show()

问题


But the graphs are not smooth because only integers are considered. enter image description here

我想要的是


如何在绘制图形时考虑整数之间的x值.

推荐答案

你可以用numpy.linspace美元.

import matplotlib.pyplot as plt
import numpy as np

xmin, xmax=-3,3

x = np.linspace(xmin, xmax, num=1000)
y,ysqr,ycube = x,x**2,x**3

f, (ax1, ax2, ax3) = plt.subplots(1,3, figsize=(25,8))

ax1.plot(x, y)
ax1.set_title('y=x')

ax2.plot(x, ysqr)
ax2.set_title('y=x^2')

ax3.plot(x, ycube)
ax3.set_title('y=x^3')

plt.show()

输出:

enter image description here

Python相关问答推荐

PyQt5如何将pyuic 5生成的Python类添加到QStackedWidget中?

如何使用bs 4从元素中提取文本

Python panda拆分列保持连续多行

基本链合同的地址是如何计算的?

如何在Deliveryter笔记本中从同步上下文正确地安排和等待Delivercio代码中的结果?

如何让 turtle 通过点击和拖动来绘制?

DataFrame groupby函数从列返回数组而不是值

如何根据参数推断对象的返回类型?

Telethon加入私有频道

try 将一行连接到Tensorflow中的矩阵

实现自定义QWidgets作为QTimeEdit的弹出窗口

当递归函数的返回值未绑定到变量时,非局部变量不更新:

UNIQUE约束失败:customuser. username

Python Pandas获取层次路径直到顶层管理

启动带有参数的Python NTFS会导致文件路径混乱

巨 Python :逆向猜谜游戏

在用于Python的Bokeh包中设置按钮的样式

如何将泛型类类型与函数返回类型结合使用?

解决Geopandas和Altair中的正图和投影问题

如何使用大量常量优化代码?