Python 无法获取预期的绘图
我从一本用python绘图的教科书中得到了一个例子 我所期望的是 但我不能做到。我得到了以下信息 我的代码:Python 无法获取预期的绘图,python,python-3.x,matplotlib,Python,Python 3.x,Matplotlib,我从一本用python绘图的教科书中得到了一个例子 我所期望的是 但我不能做到。我得到了以下信息 我的代码: import datetime as dt import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation # Create figure for plotting fig = plt.figure() ax = fig.add_subplot(1,1,1) xs
import datetime as dt
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# Create figure for plotting
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
xs = []
ys = []
# This function is called periodically from FuncAnimation
def animate (i , xs , ys ) :
tempc = round(np.random.random(), 2)
# Add x and y to lists
xs.append(dt.datetime.now().strftime('%H:%M:%S.%f'))
ys.append(tempc)
# Limit x and y lists to 20 items
xs = xs [-20:]
ys = ys [-20:]
# Draw x and y lists
ax.clear()
ax.plot(xs, ys)
# Format plot
plt.xticks(rotation =45, ha='right')
plt.subplots_adjust( bottom=0.30)
plt.title(' Temperature Data ' )
plt.ylabel( ' Temperature ( deg C) ' )
# Set up plot to call animate( ) function periodically
ani = animation.FuncAnimation(fig , animate , fargs=(xs , ys ) ,
interval =1000)
plt.show( )
我应该添加类似的for循环吗
from datetime import datetime, timedelta
试试这个:
#generate random number for temp
tempc = np.random.uniform(low=0, high=1, size=(60,))
#generate one minute timestamp
now = dt.datetime.now()
delta = timedelta(seconds=1)
for i in range(60):
xs.append(now.strftime('%H:%M:%S'))
now += delta
xs = np.array(xs)
ys = np.array(tempc)
试试这个:
#generate random number for temp
tempc = np.random.uniform(low=0, high=1, size=(60,))
#generate one minute timestamp
now = dt.datetime.now()
delta = timedelta(seconds=1)
for i in range(60):
xs.append(now.strftime('%H:%M:%S'))
now += delta
xs = np.array(xs)
ys = np.array(tempc)
您的代码对我很有用。您使用的是什么环境?什么版本的matplotlib,什么后端?我不知道。我刚刚下载了Anaconda(Windows)并将代码粘贴到Spyder(Python 3.8)窗口中,然后运行它。matplotlib:3.2.2,您的代码适合我。您使用的是什么环境?什么版本的matplotlib,什么后端?我不知道。我刚刚下载了Anaconda(Windows)并将代码粘贴到Spyder(Python 3.8)窗口中,然后运行它,