1. 程式人生 > >python筆記----matplotlib(2):條形圖、散點圖

python筆記----matplotlib(2):條形圖、散點圖

#coding=utf-8 import pandas as pd import numpy as np import matplotlib.pyplot as plt#條形圖

reviews = pd.read_csv("fandango_scores.csv") cols = ["FILM","RT_user_norm","Metacritic_user_nom","IMDB_norm","Fandango_Ratingvalue","Fandango_Stars"] norm_reviews = reviews[cols]#從矩陣取以上列

num_cols = ["RT_user_norm","Metacritic_user_nom","IMDB_norm","Fandango_Ratingvalue","Fandango_Stars"] bar_heights = norm_reviews.ix[0,num_cols].values    #取這些列的資料 作為y軸的值

from numpy import arange bar_positions = arange(5) + 0.75    #5條柱子與y軸的距離  作為x軸的值 fig,ax = plt.subplots() ax.bar(bar_positions,bar_heights,0.8)#豎圖 ax.bar繪製的是條形圖 #ax.barh(bar_positions,bar_heights,0.8)#橫圖 柱子寬度0.8 plt.show()

print("--------------------------")

#散點圖  scatter flg,ax = plt.subplots() ax.scatter(norm_reviews["Fandango_Ratingvalue"],norm_reviews["RT_user_norm"]) ax.set_xlabel("Fandango") ax.set_ylabel("Rotten Tomatoes") plt.show()

fig,ax = plt.subplots()#ax.hist(norm_reviews["Fandango_Ratingvalue"]) ax.hist(norm_reviews["Fandango_Ratingvalue"],bins = 20)#分成20個柱子#ax.hist(norm_reviews["Fandango_Ratingvalue"],range = (1,6),bins = 20)#只繪製1-6的資料 ax.set_ylin(0,50)#設定y軸範圍 plt.show()