selfteaching / selfteaching-python-camp

147 stars 880 forks source link

Day13 无法返回统计图表 #2911

Open Wangzhebin opened 5 years ago

Wangzhebin commented 5 years ago

@yjz111 程序写好后运行提示“Unused variable 'fig'。matplotlib和numpy已正常安装。程序可以运行,运行后可以正确扫码登陆。但是发动链接后无法返回图标,求教,程序附下: from mymodule import stats_word import requests from pyquery import PyQuery from wxpy import *

bot=Bot() my_friend=bot.friends().search('王小桐')

@bot.register(my_friend,SHARING)

def reply_my_friend(msg): response=requests.get(msg.url) document=PyQuery(response.text) content=document('#js_content').text() reply1=dict(stats_word.stats_txt(content,10)) import matplotlib.pyplot as plt import numpy as np np.random.seed(19680801) plt.rcdefaults() fig=plt.figure() fig,ax=plt.subplots() word=[] frequency=[] for i in reply1: word.append(i) frequency.append(reply1[i])

y_pos=np.arange(len(word))
error=np.random.rand(len(word))
plt.rcParams['font.sans-serif'] = ['SimHei']
ax.barh(y_pos,xerr=error,align='center',color='green',ecolor='black')
ax.set_yticks(y_pos)
ax.set_yticklabels(word)
ax.invert_yaxis()
ax.set_xlabel('词语出现次数')
ax.set_title('词语统计')
plt.savefig('words_frequency.png')
msg.reply_image('words_frequency.png')

embed()

srvz commented 5 years ago

把函数内的代码都放到一个 try except里捕获一下错误信息