今天整理下matplotlib常用到的图形
matplotlib
引入matplotlib包:import matplotlib.pyplot as plt
将图片内嵌在交互窗口,而不是弹出一个图片窗口
%matplotlib inline #notebook模式下
%pylab inline #ipython模式下
matplotlib官方文档
matplotlib官方文档:http://matplotlib.org/users/pyplot_tutorial.html
pyplot的api http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot
散点图(plt.scatter)
%matplotlib inline import matplotlib.pyplot as pltimport numpy as npplt.rcParams['axes.unicode_minus'] = False #解决保存图像是负号'-'显示为方块的问题#rand 均匀分布和 randn高斯分布x=np.random.randn(1,1000)y=np.random.randn(1,1000)T=np.arctan2(x,y)plt.scatter(x,y,c=T,s=25,alpha=0.5,marker='o')#c:散点的颜色#s:散点的大小#alpha:是透明程度plt.xlabel("name x")plt.ylabel("name y")plt.title("PyPlot scatter Example")#xlabel : 设置X轴的文字#ylabel : 设置Y轴的文字#title : 设置图表的标题
折线图(plt.plot)
%matplotlib inline import matplotlib.pyplot as pltimport numpy as npx1 = [1, 3, 5, 7, 9, 11, 13] y1=[30,31,33,35,47,99,480] x2 = [ 2, 4, 6, 8, 10, 12, 14] y2=[32,33,34,34,43,69,271] group_xlabels = ['8M','16M','32M','64M','128M','256M','512M']#坐标轴的刻度显示的值plt.title('this is title') plt.xlabel('this is xlables') plt.ylabel('this is ylables') plt.plot(x1, y1,'r') plt.plot(x2, y2,'b') plt.xticks(x1, group_xlabels, rotation=0) #plt.xticks(),plt.yticks()设置轴记号(设置坐标轴的刻度显示的值).
条形图(plt.bar)
%matplotlib inline import matplotlib.pyplot as pltimport numpy as npplt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签,有中文出现的情况,需要u'内容'x1 = [1, 3, 5, 7, 9, 11, 13] y1=[30,31,33,35,47,99,480] group_xlabels = [u'星期一',u'星期二',u'星期三',u'星期四',u'星期五',u'星期六',u'星期日']plt.title('this is title') plt.xlabel('this is xlabels') plt.ylabel('this is ylabels') plt.bar(x1,y1,width=1) plt.xticks(x1, group_xlabels, rotation=0)
饼图(plt.pie)
import matplotlib.pyplot as pltimport pandas as pd%matplotlib inlinemovie_df=pd.read_csv("C:/Users/zzy/Desktop/movie_metadata.csv")#movie_df.head()def high_score_area(df): df1=df.where(df['imdb_score']>=8) p1=df1.groupby('country').movie_title.count().head(20) p1.plot.pie() high_score_area(movie_df)
df=movie_dfdf1=df.where(df['imdb_score']>=8)p1=df1.groupby('country').movie_title.count().head(5)label_list=p1.indexplt.title('pie chart')plt.pie(p1,labels=label_list,autopct='%1.1f%%')
直方图(plt.hist)
import numpy as npimport matplotlib.pyplot as plt%matplotlib inlinemu, sigma = 100, 15x = mu + sigma * np.random.randn(10000)# 数据的直方图plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75)plt.xlabel('Smarts')plt.ylabel('Probability')plt.title('Histogram of IQ')#添加文字plt.text(60, .025, r'$\mu=100,\ \sigma=15$')#固定坐标轴区间,X轴定在40-160,Y轴定在0-0.03plt.axis([40, 160, 0, 0.03])plt.grid(True)plt.show()
箱线图(plt.box)
import numpy as npimport matplotlib.pyplot as pltimport pandas as pdnp.random.seed(2) #设置随机种子df = pd.DataFrame(np.random.rand(5,4),columns=['A', 'B', 'C', 'D'])#先生成0-1之间的5*4维度数据,再装入4列DataFrame中plt.xlabel('whataver')plt.ylabel('seed number')plt.title('box chart')df.boxplot(patch_artist=True) #也可用plot.box()
df.boxplot(sym='r*',vert=False,patch_artist=True,meanline=False,showmeans=True)plt.show()
其中,sym='r*',表示异常点的形状,
vert=False,表示横向还是竖向(True),,
patch_artist=True,(上下四分位框内是否填充,True为填充)
meanline=False,showmeans=True,是否有均值线及其形状,meanline=True时,均值线也像中位数线一样是条红色线段,这样容易与中位数线混淆。 另外,还有其他参数,比如notch表示中间箱体是否缺口,whis为设置数据的范围,showcaps、showbox是否显示边框,可以参见