画图方法总结

python绘图总结

绘图1

# -*- coding: utf-8 -*- 
import matplotlib.pyplot as plt
import numpy as np
docnames=['X1','X2','TX3','X4','X5']
doctopic=[[0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1],[0.09,0.11,0.09,0.11,0.09,0.11,0.09,0.11,0.09,0.11],[0.12,0.08,0.12,0.08,0.12,0.08,0.12,0.08,0.12,0.08],[0.14,0.06,0.14,0.06,0.14,0.06,0.14,0.06,0.14,0.06],[0.15,0.05,0.15,0.05,0.15,0.05,0.15,0.05,0.15,0.05]]
doctopic=np.array(doctopic)
N, K = doctopic.shape
ind = np.arange(N)
width = 0.5
plots = []
height_cumulative = np.zeros(N)
for k in range(K):
	color = plt.cm.coolwarm(k/K, 1)
	if k == 0:
		p = plt.bar(ind, doctopic[:, k], width, color=color)
	else:
		p = plt.bar(ind, doctopic[:, k], width, bottom=height_cumulative, color=color)
	height_cumulative += doctopic[:, k]
	plots.append(p)
plt.ylim((0, 1))
plt.ylabel('Probability')
plt.title('SAMPLE 1')
plt.xticks(ind, docnames)   #改变X轴示例位置
plt.yticks(np.arange(0, 0.1, 1))  #改变Y轴示例位置
topic_labels = ['X{}'.format(k) for k in range(K)]
plt.legend([p[0] for p in plots], topic_labels,loc='center right')
plt.show()

运行结果:

画图方法总结_第1张图片

绘图2

# -*- coding: utf-8 -*- 
import matplotlib.pyplot as plt
import numpy as np
import openpyxl
from pylab import *
from matplotlib.ticker import MultipleLocator, FormatStrFormatter

docnames=['Austen_Emma','Austen_Pride','Austen_Sense','CBronte_Jane','CBronte_Professor','CBronte_Villette']
doctopic=[[ 0.0625,  0.1736,  0.0819,  0.4649,  0.2171],[ 0.0574,  0.1743,  0.0835,  0.4008,  0.2839],[ 0.0599,  0.1645,  0.0922,  0.2034,  0.4801],[ 0.189 ,  0.1897,  0.3701,  0.1149,  0.1362],[ 0.2772,  0.2681,  0.2387,  0.0838,  0.1322],[ 0.3553,  0.193 ,  0.2409,  0.0865,  0.1243]]


xmajorLocator   = MultipleLocator(1) #将x主刻度标签设置为20的倍数
xmajorFormatter = FormatStrFormatter('%d') #设置x轴标签文本的格式
xminorLocator   = MultipleLocator(10) #将x轴次刻度标签设置为5的倍数


doctopic=np.array(doctopic)

doctopic=doctopic.transpose()

N, K = doctopic.shape
#topic_labels = ['No #{}'.format(k) for k in range(K)]
#topic_labels=['0','30','60','90','120','150','180','210','240']

plt.pcolor(doctopic, norm=None, cmap='Blues')#Blues
plt.yticks(np.arange(doctopic.shape[0])+0.5, docnames)
plt.xticks(np.arange(doctopic.shape[1])) #topic_labels,rotation=0
plt.gca().invert_yaxis()
plt.xticks(rotation=90)
plt.colorbar(cmap='Blues') #Oranges,Purples,Reds,hot
plt.tight_layout()

ax=plt.subplot(111)
ax.xaxis.set_major_locator(xmajorLocator)
ax.xaxis.set_major_formatter(xmajorFormatter)

plt.show()

运行结果:

画图方法总结_第2张图片

 

 

你可能感兴趣的:(Python,绘图)