1、準備工作
# These are the "Tableau 20" colors as RGB.
tableau20 = [(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),
(44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150),
(148, 103, 189), (197, 176, 213), (140, 86, 75), (196, 156, 148),
(227, 119, 194), (247, 182, 210), (127, 127, 127), (199, 199, 199),
(188, 189, 34), (219, 219, 141), (23, 190, 207), (158, 218, 229)]
# Scale the RGB values to the [0, 1] range, which is the format matplotlib accepts.
for i in range(len(tableau20)):
r, g, b = tableau20[i]
tableau20[i] = (r / 255., g / 255., b / 255.)
#設置圖的大小
plt.figure(figsize=(12, 14))
#限制座標軸的範圍,防止出現大片空白
plt.ylim(0, 90)
plt.xlim(1968, 2014)
#x,y上的數據名稱
plt.xlabel("x")
plt.ylabel("y")
#去掉上下左右的黑色框線
ax = plt.subplot(111)
ax.spines["top"].set_visible(False)
ax.spines["bottom"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines["left"].set_visible(False)
#座標軸上的數字出現在上還是下,左還是右?
#ax.get_xaxis().tick_top()
ax.get_xaxis().tick_bottom()
#ax.get_yaxis().tick_left()
ax.get_yaxis().tick_right()
#調整座標軸上的字體以及格式
plt.yticks(range(0, 91, 10), [str(x) + "%" for x in range(0, 91, 10)], fontsize=14) #第一個參數是文字位置,第二個參數是文字
plt.xticks(fontsize=14)
#右上角的圖例,元組形式
plt.legend((rect,),(u<span style="color:#ff00bf;">"圖例"</span>,))
#沿着每個座標繪製虛線,方便查看座標值
for y in range(10, 91, 10):
plt.plot(range(1968, 2012), [y] * len(range(1968, 2012)), "--", lw=0.5, color="black", alpha=0.3)
#去掉座標上的數字和小線,top等是去掉tick mark,labelbottom是下邊的文字標記
plt.tick_params(axis="both", which="both", bottom="off", top="off",
labelbottom="on", left="off", right="off", labelleft="on")
2、曲線
</pre><pre name="code" class="python">
plt.plot(xilst, ylist, lw=2.5, color=tableau20[0])
#在每個線的後面加上描述,其實就是指定位置添加文本
plt.text(x_pos, y_pos, info, fontsize=14, color=tableau20[rank])
plt.text(1995, 93, "Percentage of Bachelor's degrees conferred to women in the U.S.A., by major (1970-2012)", fontsize=17, ha="center")
在圖裏麪包含數據來源以及版權:
plt.text(1966, -8, "Data source: nces.ed.gov/programs/digest/2013menu_tables.asp"
"\nAuthor: Randy Olson (randalolson.com / @randal_olson)"
"\nNote: Some majors are missing because the historical data "
"is not available for them", fontsize=10)
保存成png格式或其他:
#bbox_inches="tight"表示去除邊緣部分的空白
plt.savefig("percent-bachelors-degrees-women-usa.png", bbox_inches="tight")
3、直方圖
data = list(np.random.randn(10000))
data1 = list(2*np.random.randn(10000))
info = r'$\mu=0.1, \ \sigma= %f$' % (0.2)
plt.text(1, 0.1, info, bbox=dict(facecolor='red', alpha=0.25))#前兩個值表示文本框放置的位置
plt.hist(data, 50, normed=True, facecolor='r', alpha=1)#50表示把數據的區間分成多少份進行統計, normed指求得是頻數還是頻率,alpha都表示透明程度,越小越透明</span>
plt.hist(data1, 100, normed=True, facecolor='g', alpha=0.8)
plt.grid(True)
plt.show()
現在plot.ly提供了交互的動態圖,只需要添加一行代碼即可。
4、實踐一:繪製confusion matrix
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
import numpy as np
def makeconf(conf_arr, model_name):
# makes a confusion matrix plot when provided a matrix conf_arr
# every row of conf_arr is normalized
norm_conf = []
for i in conf_arr:
a = 0
tmp_arr = []
a = sum(i, 0)
for j in i:
tmp_arr.append(float(j)/float(a))
norm_conf.append(tmp_arr)
fig = plt.figure()
plt.clf() #清除畫布
ax = fig.add_subplot(111) #參數的意思是把畫布分成1行1列,把圖畫在第1塊(從上到下從左到右數起)。也可以寫成 fig.add_subplot(1,1,1)
ax.set_aspect(1) #控制縱橫比,1:1
res = ax.imshow(np.array(norm_conf), cmap=plt.cm.jet,
interpolation='nearest') #根據np array的數組繪製圖,第二個參數是配色,有jet、gray
width = len(conf_arr)
height = len(conf_arr[0])
for x in xrange(width):
for y in xrange(height):
ax.annotate(str(conf_arr[x][y]), xy=(y, x),
horizontalalignment='center',
verticalalignment='center') #在每一塊表上數字,第一個參數是要標上的字符串,第二個是座標
cb = fig.colorbar(res) #在圖的旁邊繪製一個bar,展示顏色代表的數值
indexs = '0123456789'
plt.xticks(range(width), indexs[:width]) #x, y軸的座標名
plt.yticks(range(height), indexs[:height])
# you can save the figure here with:
# plt.savefig("pathname/image.png")
plt.savefig("conf_matrix/{}_confusion_matrix.png".format(model_name))
if __name__=="__main__":
y = [1,0,1,1,1,0,0]
predicts = [1,1,0,1,0,1,0]
conf_matrix = confusion_matrix(y, predicts)
print conf_matrix
makeconf(conf_matrix, "test")
問題:
想在繪製的圖中顯示中文信息,需要修改配置文件:/usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data/matplotlibrc
把font.family和font.sans.seris前面的#去掉,並在font.sans.seris的第一個位置加上ubuntu已經安裝的中文字體:Droid Sans Fallback。如果需要查看ubuntu下有哪些中文字體:
fc-list :lang=zh-cn
另外ubuntu中常見的中文字體是文泉驛微黑。
參考