1. 程式人生 > >Pandas條形圖與散點圖(續)

Pandas條形圖與散點圖(續)

import matplotlib.pyplot as plt
from numpy import arange
import pandas as pd

 

reviews = pd.read_csv(r"E:\PyCharm\fandango_score_comparison.csv")
cols = ["FILM", "RT_user_norm", "Metacritic_user_nom", "IMDB_norm", "Fandango_Ratingvalue", "Fandango_Stars"]

num_cols = ["RT_user_norm", "Metacritic_user_nom", "IMDB_norm", "Fandango_Ratingvalue", "Fandango_Stars"]
norm_reviews = reviews[cols]

 

bar_width = norm_reviews.ix[0, num_cols].values

bar_positions = arange(5) + 0.75

fig, ax = plt.subplot()

ax.barh(bar_positions, bar_width, 0.5)                                #設定成入下格式

結果如下:

 

設定柱狀圖如下:

tick_positions = range(1,6)

ax.set_yticks(tick_positions)   

ax.set_yticklabels(num_cols)     #這兩句話用來設定Y軸上的文字

 

ax.set_ylabel("Rating Source")

ax.set_xlabel("Average Rating")

ax.set_title("Analyse")

plt.show()

結果如下:

 

散點圖的繪製如下:

fig, ax = plt.subplots()

ax.scatter(norm_reviews["RT_user_norm"], norm_reviews["Metacritic_user_nom"])

ax.set_xlabel("RT_user_norm")
ax.set_ylabel("Metacritic_user_nom")
plt.show()

結果如下:

 

fig = plt.subplot(figsize(5,5))

ax1 = fig.add_subplot(2,2,1)

ax2 = fig.add_subplot(2,2,4)

 

ax1.scatter(norm_reviews["RT_user_norm"], norm_reviews["Metacritic_user_nom"])

ax1.set_xlabel("RT_user_norm")

ax1.set_ylabel("Metacritic_user_nom")

 

ax2.scatter(norm_reviews["RT_user_norm"], norm_reviews["Metacritic_user_nom"])

ax2.set_xlabel("RT_user_norm")

ax2.set_ylabel("Metacritic_user_nom")

plt.show()                                   #fig設定圖的引數, ax通常進行實際操作

結果如下: