您的位置:首页 > 编程语言 > Python开发

【Python学习笔记 】12.可视化库Matplotlib(下)

2018-04-04 17:54 1141 查看
3.条形图和散点图
    (1).条形图

    fig,ax=plt.subplots()  创建子图

    ax.bar(bar_positon,bar_heights,0.3)    创建条形图,位置分别为bar_position。高度分别为bar_heights,宽度为0.3。

    若把ax.bar改成ax.barh 则条形图横放,如下图。

    


    


    .barh版:

    


    (2).散点图

    fig,ax=plt.subplots()  fig,ax分别等于plt.subplots()返回的两个值。fig返回plt.figure()。

    ax.scatter(x,y)  画出横坐标为x,纵坐标为y的散点图。

    


        


    


    


4.柱形图与盒图。
    (1).柱形图

    柱形图指带Bins的图。

    ax.hist()指画的图带Bins结构,图中不指名bins等于多少时,图中自动分bins,range(a,b)指起始位置分别为a,b

    


        


        


        


    axi.set_ylim(0,50)指设置y的区间为(0,,50)import matplotlib.pyplot as plt
import pandas as pd
from numpy import arange
reviews=pd.read_csv('fandango_scores.csv')
cols=['RT_user_norm','Metacritic_user_nom','IMDB_norm','Fandango_Ratingvalue','Fandango_Stars']

norm_reviews=reviews[cols]

fig=plt.figure(figsize=(5,20))
ax1=fig.add_subplot(4,1,1)
ax2=fig.add_subplot(4,1,2)
ax3=fig.add_subplot(4,1,3)
ax4=fig.add_subplot(4,1,4)
ax1.hist(norm_reviews['Fandango_Ratingvalue'],bins=20,range=(0,5))
ax1.set_title('DFR')
ax1.set_ylim(0,50)

ax2.hist(norm_reviews['RT_user_norm'],20,range=(0,5))
ax2.set_title('DRT')
ax2.set_ylim(0,50)

ax3.hist(norm_reviews['Metacritic_user_nom'],20,range=(0,5))
ax3.set_title('MUN')
ax3.set_ylim(0,50)

ax4.hist(norm_reviews['IMDB_norm'],20,range=(0,5))
ax4.set_title('IN')
ax4.set_ylim(0,50)

plt.show()    


    (2)盒图

    盒图指提取出数据某列特征四分之一,二分之一,四分之三处的值
import matplotlib.pyplot as plt
import pandas as pd
from numpy import arange
reviews=pd.read_csv('fandango_scores.csv')
cols=['RT_user_norm','Metacritic_user_nom','IMDB_norm','Fandango_Ratingvalue','Fandango_Stars']

norm_reviews=reviews[cols]

fig,ax=plt.subplots()
ax.boxplot(norm_reviews[cols].values)
ax.set_xticklabels(cols,rotation=90)
ax.set_ylim(0,5)
plt.show()

    


    画多个盒图:import matplotlib.pyplot as plt
import pandas as pd
from numpy import arange
reviews=pd.read_csv('fandango_scores.csv')
cols=['RT_user_norm','Metacritic_user_nom','IMDB_norm','Fandango_Ratingvalue','Fandango_Stars']

norm_reviews=reviews[cols]

fig,ax=plt.subplots()
ax.boxplot(norm_reviews[cols].values)
ax.set_xticklabels(cols,rotation=90)
ax.set_ylim(0,5)
plt.show()
    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: