python - 指数图在python中显示为线性图
问题描述
我试图将 2 与 2 的指数绘制成指数的幂,但我一直得到一个线性图而不是曲线。我不确定我做错了什么。
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
from random import seed
from random import random
import math
tiktok=0
#Desired style
style.use('dark_background')
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
T= open('live_graphText.txt','w+')
T.truncate(0)
T.close()
test = f=open('live_graphText.txt','w+')
for i in range(10):
test.write("%d,%d\n"%(i,math.pow(2,i)))
tiktok = tiktok+i
test.close()
此外,我正在尝试将 matplotlib.animation 中的实时图形动画与我创建的文件一起使用。保持自动继续向文件添加点,但似乎该函数甚至没有被调用。我不确定我是在错误的地方调用它还是这个函数没有意义
#This is not being called
def edit(tik):
global tiktok
f=open('live_graphText.txt','a+')
f.write("%d,%d\n"%(tik,math.pow(2,tik)))
print("HelloWorld")
tiktok = tiktok +1
f.close()
def animate(i):
#Opening the file to read
graph_data = open('live_graphText.txt','r').read()
#Split the lines by \n
lines=graph_data.split('\n')
xs =[]
ys =[]
for line in lines:
#This if statement ignores any white space at the end of the file
if len(line)>1:
x,y = line.split(',')
xs.append(x)
ys.append(y)
#print(xs)
#print(ys)
ax1.clear()
ax1.plot(xs,ys)
#参数是(在哪里绘制函数,我们要绘制的函数,我们要绘制的间隔以毫秒为单位)
ani = animation.FuncAnimation(fig,animate,interval=1000)
plt.show()
while tiktok<20:
edit(tiktok)
print(tiktok)
plt.show()
任何帮助表示赞赏!
解决方案
推荐阅读
- c++ - openMP:并行运行所有线程会导致内存不足异常
- reactjs - TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。接收类型未定义
- r - 在 R 中查找反导数
- css - 如何正确地从 material-components-web 更改 Sass 样式?
- python - 未捕获的类型错误:无法读取未定义的属性“colspan”
- sql-server - 使用 Xamarin.Android 从相机获取照片并将其保存在 SQL Server 中
- kotlin - 将列表分块为连续相同对象的列表
- ios - 迅速。将 Hasher.combine 与字符串结合使用时,调用中缺少参数标签“字节:”
- java - 为什么要爆炸不包括 spring-boot-starter-logging 的日志文件?
- gcc - 本地构建的 GCC 交叉编译器使用 -D_FORTIFY_SOURCE=2 报告 MB_LEN_MAX 错误