Matplotlib常用画图代码2

时间:2023-01-18 15:23:34

多条柱状图,中文,图例

代码块

把文章里面用到的代码都粘出来,不然重装系统丢掉后修改文章重画的工作量太大:

# -*- coding: utf-8 -*-
import numpy as np
from matplotlib import pyplot as plt
import matplotlib

if __name__ == "__main__":
zhfont = matplotlib.font_manager.FontProperties(fname='/usr/share/fonts/truetype/wqy/wqy-microhei.ttc',size=18)
matplotlib.rcParams['axes.unicode_minus'] = False
plt.figure(figsize=(9,6))
n = 4
X = np.arange(n)+1

plt.clf()
Y1 = [0.731,0.894,0.727,0.820]
Y2 = [0.750,0.914,0.735,0.838]
plt.xticks([1.35,2.35,3.35,4.35], ['S','S+A','S+P','S+C'],size=18)
plt.yticks([0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0],size=18)
plt.xlabel(u'特征组合',fontproperties=zhfont,size=18)
plt.ylabel(u'指标值',fontproperties=zhfont,size=18)
b1 = plt.bar(X,Y1,width = 0.35,facecolor = 'white',edgecolor = 'black')

b2 = plt.bar(X+0.35,Y2,width = 0.35,facecolor = 'black',edgecolor = 'black')
plt.legend([b1,b2], [u"准确率",u"F 值"],loc='lower right',prop=zhfont)

plt.ylim(0.0,1.0)
plt.show()

Matplotlib常用画图代码2

三点度,幂率分布图绘制

代码块

# -*- coding: utf-8 -*-
import time, os
import re
import json
import random
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from pymongo import MongoClient

import types
import math
import matplotlib.pyplot as plt
def popu_scatter():
client = MongoClient("localhost", 27017)
db = client['NetEase']
table = db['news_popu_finished']

p = re.compile(r'[0-9]+')
commCnt_rem = []

for news in table.find():
commCnt = news['commCount']
if commCnt is None:
continue
elif type(commCnt) == types.IntType:
commCnt_rem.append(commCnt)
elif '-' in commCnt:
continue
else:
#print commCnt
commCnt = p.findall(commCnt)[0]
commCnt_rem.append(commCnt)
client.close()
commDict = {}
for item in commCnt_rem:
if int(item) not in commDict:
commDict[int(item)] = 1
else:
commDict[int(item)] = commDict[int(item)] + 1
target_cnt = 0
for item in commDict:
if item >= 0:
target_cnt = target_cnt + commDict[item]
print target_cnt
#print commDict
X = []
Y = []
for item in commDict:
#print item
X.append(math.log10(float(item+1)))
Y.append(math.log10(float(commDict[item])+1))

plt.plot(X,Y,'ok',markersize=3,alpha=0.5)
plt.xlim(0,7)
plt.ylim(0,4)
plt.ylabel(u'# news', fontsize = 12, rotation=90)
plt.xlabel(u'# views',fontsize = 12)
plt.show()

if __name__ == "__main__":
popu_scatter()

Matplotlib常用画图代码2