简体   繁体   English

Matplotlib 图表不显示

[英]Matplotlib chart does not display

The following code write string 1 or -1 into a.txt file based on Tweeter API Tweet about a given word ('Bitcoin' in this case).以下代码基于 Tweeter API 将字符串1-1写入 a.txt 文件中关于给定单词(在本例中为“比特币”)的推文。 Then I use Matplotlib animate to display the data which are downloaded continuously from Twitter api.然后我使用 Matplotlib 动画显示从 Twitter api 连续下载的数据。 The data are Tweet but I attribute 1 or -1 to each Tweet downloaded so that it can be used on a chart.数据是 Tweet,但我将 1 或 -1 分配给每个下载的 Tweet,以便可以在图表上使用。 My question is, do you understand why the chart does not display while the data are properly downloading and written into the.txt file.我的问题是,您是否理解为什么在数据正确下载并写入 .txt 文件时图表不显示。 I have no error.我没有错误。 The program does not end since it is downloading in continue.该程序没有结束,因为它正在继续下载。 Any contribution please?请问有什么贡献吗?

auth = tweepy.OAuthHandler(ckey, csecret)
auth.set_access_token(akey, atoken)
api = tweepy.API(auth,wait_on_rate_limit=True)


class listener(StreamListener):
    def on_data(self, data):
        all_data = json.loads(data)

        tweet = all_data["text"]
        sentiment_value = str(m.sentiment(tweet))
    
        output = open("twitter-out.txt","a")
        output.write(sentiment_value)
        output.write('\n')
        output.close()
        

        return True

    def on_error(self, status):
        print(status)

twitterStream = Stream(auth, listener())
twitterStream.filter(track=["Bitcoin"])


''' plot live sentiment '''
style.use("ggplot")
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

def animate(i):
    pullData = open("twitter-out.txt","r").read()
    lines = pullData.split('\n')

    xar = []
    yar = []

    x = 0 
    y = 0 

    for l in lines:
        x += 1
        if "1" in l:
            y += 1
        elif "-1" in l:
            y -= 1

        xar.append(x)
        yar.append(y)

    ax1.clear()
    ax1.plot(xar,yar)

ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()

I have figured out the solution.我已经想出了解决方案。 The second function which is in charge of displaying the data inside the.txt file has to be in a separate.py file in order to call the continuously running Twitter api calls.第二个 function 负责显示 .txt 文件中的数据,必须在单独的.py 文件中才能调用连续运行的 Twitter api 调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM