python用函数绘制椭圆_python之Matplotlib画图---中文乱码,椭圆以及高斯混合模型作图。...

1. 中文乱码和椭圆先看python代码和结果:import matplotlibfrom matplotlib.patches import Ellipse, Circleimport matplotlib.pyplot as pltfig, ax = plt.subplots()zhfont1 = matplotlib.font_manager.FontProperties(fname='C:\Windows\Fonts\simsun.ttc')ell1 = Ellipse(xy = (2.0, 2.0), width = 4, height = 8, angle = 30.0, facecolor= 'yellow', alpha=0.3)ell2 = Ellipse(xy = (2.0, -2.0), width = 2, height = 4, angle = 60.0, facecolor= 'silver', alpha=0.3)ell3 = Ellipse(xy = (-2.0, -2.0), width = 2, height = 4, angle = 90.0, facecolor= 'maroon', alpha=0.3)cir1 = Circle(xy = (-2.0, 2.0), radius=2, alpha=0.5)ax.text(2.0, 2.0, '椭圆1', fontsize=12,fontproperties=zhfont1) ax.text(2.0, -2.0, '椭圆2', fontsize=12,fontproperties=zhfont1) ax.text(-2.0, -2.0, '椭圆3', fontsize=12,fontproperties=zhfont1) ax.text(-2.0, 2.0, '圆1', fontsize=12,fontproperties=zhfont1) ax.add_patch(ell1)ax.add_patch(ell2)ax.add_patch(ell3)ax.add_patch(cir1)ax.set_xlim(-5, 5)ax.set_ylim(-5, 5)plt.xlabel('性别',fontproperties=zhfont1)plt.figure()只要在后面加上fontproperties=zhfont1就可以解决中文乱码问题了。其中simsun.ttc就是你电脑里面的字体文件,可以更换。 Ellipse方法是建立一个椭圆对象,ax.text是给椭圆加上说明文字,add_patch是画椭圆的,具体机制不太清楚。 参考链接: Python图表绘制——matplotlib绘图库入门:http://www.cnblogs.com/wei-li/archive/2012/05/23/2506940.html Python数据可视化——散点图:http://blog.csdn.net/abcjennifer/article/details/198482692. 高斯混合模型作图高斯混合模型的介绍见如下链接: http://www.dataivy.cn/blog/%E6%B7%B7%E5%90%88%E9%AB%98%E6%96%AF%E6%A8%A1%E5%9E%8Bgaussian-mixture-model_gmm/ 高斯混合模型(也叫GMM),与k-means的不同点在于:k-means 的结果只是分类标签,而 GMM 还给出的是被分类到不同类别中的概率。 GMM的作图非常有趣和高大上,结果和部分代码如下: def Draw2DGaussians(gaussianMixtureModel, ellipseColors, ellipseTextMessages):

fig, h = plt.subplots();

for i, (mean, covarianceMatrix) in enumerate(zip(gaussianMixtureModel.means_, gaussianMixtureModel._get_covars())):

h.text(mean[0]+7, mean[1]-1, ellipseTextMessages[i], fontsize=12)

# get the eigen vectors and eigen values of the covariance matrix

v, w = np.linalg.eigh(covarianceMatrix)

v = 2.5*np.sqrt(v) # go to units of standard deviation instead of variance

# calculate the ellipse angle and two axis length and draw it

u = w[0] / np.linalg.norm(w[0])

angle = np.arctan(u[1] / u[0])

angle = 180 * angle / np.pi

# convert to degrees

currEllipse = mpl.patches.Ellipse(mean, v[0], v[1], 180 + angle, color=ellipseColors[i])

currEllipse.set_alpha(0.5)

h.add_patch(currEllipse)plt.figure()plt.rcParams['figure.figsize'] = (11, 8)ellipseTextMessages = [str(100*gaussianMixtureModel.weights_[x])[:4]+'%' for x in range(numGaussians)]ellipseColors = ['red','green','purple','cyan','magenta','yellow','blue','orange','silver','maroon','lime','olive','brown','darkblue']Draw2DGaussians(gaussianMixtureModel, ellipseColors, ellipseTextMessages)上图表示的是kobe在各个区域投篮的概率。 数据集用的kaggle上kobe投篮预测的比赛数据集。篮筐等是另外画的,代码中的函数只是画了那11个椭圆。数据集可以更换,效果一样。 gaussianMixtureModel是建立的高斯混合模型,一共有11个类别;ellipseColors是各类别画图时的颜色,ellipseTextMessages各类别画图时的说明信息(比如图中的12.2%)。 参考链接: Psychology of a Professional Athlete:https://www.kaggle.com/selfishgene/kobe-bryant-shot-selection/psychology-of-a-professional-athlete/comments

$(function () {

$('pre.prettyprint code').each(function () {

var lines = $(this).text().split('\n').length;

var $numbering = $('

').addClass('pre-numbering').hide();

$(this).addClass('has-numbering').parent().append($numbering);

for (i = 1; i <= lines; i++) {

$numbering.append($('

').text(i));

};

$numbering.fadeIn(1700);

});

});

你可能感兴趣的:(python用函数绘制椭圆)