def show_user_age_pie(): map = userDs.get_country_user_age_data() data =[] labels =[] for k in map: labels.append(k) data.append(map[k]) colors = 'yellowgreen', 'gold', 'lightskyblue', 'lightcoral', 'orange', 'yellow', 'red', 'pink' explode = 0, 0, 0, 0, 0.1, 0, 0 font = FontProperties(fname='/Library/Fonts/Songti.ttc', size=10) pie = plt.pie(data, labels=labels, explode=explode, colors=colors, autopct='%1.1f%%', shadow=False, startangle=40, textprops={'fontsize': 12}) # 解决标题中文乱码 plt.title("keep用户年龄分布", fontproperties=font) # 解决乱码问题 for font in pie[1]: font.set_fontproperties(matplotlib.font_manager.FontProperties(fname='/Library/Fonts/Songti.ttc')) plt.axis('equal') # plt.rcParams['axes.unicode_minus']=False #用来正常显示负号 # # plt.rcParams['font.family']=['Arial Black'] plt.show()
def caculateWordFrequency(): ''' 统计词频,画柱形图、饼状图 :return: ''' global wordList, wordFrequency ## 统计词频,储存在wordFrequency dict里面 for word in wordList: if word in wordFrequency.keys(): wordFrequency[word] += 1 else: wordFrequency[word] = 1 ## 按value值排序 items = wordFrequency.items() sortedItems = [[item[1], item[0]] for item in items] sortedItems.sort() ## 顺序 sortedItems.reverse() ## 降序 ## 取前30个,画柱形图 font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14) ##字体设置 X_labels = [sortedItems[i][1] for i in range(0, 30)] Y = [sortedItems[i][0] for i in range(0, 30)] X = range(len(X_labels)) fig = plt.figure() plt.bar(X, Y, color="green") plt.xticks(X, X_labels, fontproperties=font) plt.xlabel("X-axis") plt.ylabel("Y-axis") plt.title("TOP 30th words") plt.show() ## 取前30个,画饼状图 labels = [sortedItems[i][1] for i in range(0, len(sortedItems))] ### 求比例 totalNum = 0 rates = [] for sortedItem in sortedItems: totalNum += sortedItem[0] for i in range(0, len(sortedItems)): rate = sortedItems[i][0] / totalNum * 100 rate = round(rate, 10) rates.append(rate) ### 取前30个词语 labels = labels[0:30] rates = rates[0:30] ### 作图 explode = [0] * 30 explode[0] = explode[1] = 0.1 ## 突出第一、二个 fig = plt.figure() plt.axes(aspect=1) patches, l_text, p_text = plt.pie(x=rates, labels=labels, explode=explode, autopct='%3.1f %%', shadow=True, labeldistance=1.1, startangle=90, pctdistance=0.8) ### 中文显示问题 for font in l_text: font.set_fontproperties( FontProperties(fname=r"c:\windows\fonts\simsun.ttc")) plt.show()
def caculateWordFrequency(): ''' 统计词频,画柱形图、饼状图 :return: ''' global wordList,wordFrequency ## 统计词频,储存在wordFrequency dict里面 for word in wordList: if word in wordFrequency.keys(): wordFrequency[word] += 1 else: wordFrequency[word] = 1 ## 按value值排序 items = wordFrequency.items() sortedItems = [ [item[1],item[0]] for item in items ] sortedItems.sort() ## 顺序 sortedItems.reverse() ## 降序 ## 取前30个,画柱形图 font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14) ##字体设置 X_labels = [ sortedItems[i][1] for i in range(0,30) ] Y = [ sortedItems[i][0] for i in range(0,30) ] X = range(len(X_labels)) fig = plt.figure() plt.bar(X, Y, color="green") plt.xticks(X, X_labels,fontproperties=font) plt.xlabel("X-axis") plt.ylabel("Y-axis") plt.title("TOP 30th words") plt.show() ## 取前30个,画饼状图 labels = [ sortedItems[i][1] for i in range(0,len(sortedItems)) ] ### 求比例 totalNum = 0 rates = [] for sortedItem in sortedItems: totalNum += sortedItem[0] for i in range(0,len(sortedItems)): rate = sortedItems[i][0] / totalNum * 100 rate = round(rate,10) rates.append(rate) ### 取前30个词语 labels = labels[0:30] rates = rates[0:30] ### 作图 explode = [0] * 30 explode[0] = explode[1] = 0.1 ## 突出第一、二个 fig = plt.figure() plt.axes(aspect=1) patches,l_text,p_text = plt.pie( x=rates, labels=labels, explode=explode, autopct='%3.1f %%', shadow=True, labeldistance=1.1, startangle=90, pctdistance=0.8 ) ### 中文显示问题 for font in l_text: font.set_fontproperties(FontProperties(fname=r"c:\windows\fonts\simsun.ttc")) plt.show()
def show_user_age_pie(): cursor.execute(birthday_sql) result = cursor.fetchall() tag_60s = '50,60后' tag_70s = '70后' tag_80s = '80后' tag_85s = '85后' tag_90s = '90后' tag_95s = '95后' tag_00s = '00后' labels = tag_60s, tag_70s, tag_80s, tag_85s, tag_90s, tag_95s, tag_00s map = { tag_60s: 0, tag_70s: 0, tag_80s: 0, tag_85s: 0, tag_90s: 0, tag_95s: 0, tag_00s: 0 } for item in result: bd = item['bd'] count = item['count'] if 1950 <= bd < 1970: map[tag_60s] = map[tag_60s] + count pass elif 1970 <= bd < 1980: map[tag_70s] = map[tag_70s] + count pass elif 1980 <= bd < 1985: map[tag_80s] = map[tag_80s] + count elif 1985 <= bd < 1990: map[tag_85s] = map[tag_85s] + count pass elif 1990 <= bd < 1995: map[tag_90s] = map[tag_90s] + count pass elif 1995 <= bd < 2000: map[tag_95s] = map[tag_95s] + count pass elif 2000 <= bd < 2010: map[tag_00s] = map[tag_00s] + count pass pass sizes = [ map[tag_60s], map[tag_70s], map[tag_80s], map[tag_85s], map[tag_90s], map[tag_95s], map[tag_00s] ] colors = 'yellowgreen', 'gold', 'lightskyblue', 'lightcoral', 'orange', 'yellow', 'red', 'pink' explode = 0, 0, 0, 0, 0, 0.1, 0, 0 font = FontProperties(fname='/Library/Fonts/Songti.ttc', size=10) pie = plt.pie(sizes, labels=labels, explode=explode, colors=colors, autopct='%1.1f%%', shadow=False, startangle=40, textprops={'fontsize': 12}) # 解决标题中文乱码 plt.title("keep用户年龄分布", fontproperties=font) # 解决乱码问题 for font in pie[1]: font.set_fontproperties( matplotlib.font_manager.FontProperties( fname='/Library/Fonts/Songti.ttc')) plt.axis('equal') # plt.rcParams['axes.unicode_minus']=False #用来正常显示负号 # # plt.rcParams['font.family']=['Arial Black'] plt.show()