04Python数据可视化库-Matplotlib

import pandas as pd

unrate = pd.read_csv('unrate.csv')

unrate['DATE'] = pd.to_datetime(unrate['DATE'])       #将DATE栏里的数据用时间的格式表示出来

print(unrate.head(12))                 


         DATE  VALUE
0  1948-01-01    3.4
1  1948-02-01    3.8
2  1948-03-01    4.0
3  1948-04-01    3.9
4  1948-05-01    3.5
5  1948-06-01    3.6
6  1948-07-01    3.6
7  1948-08-01    3.9
8  1948-09-01    3.8
9  1948-10-01    3.7
10 1948-11-01    3.8
11 1948-12-01    4.0


import matplotlib.pyplot as plt

plt.plot()

plt.show()




first_twelve = unrate[0:12] 

plt.plot(first_twelve['DATE'], first_twelve['VALUE'])

plt.show()



plt.plot(first_twelve['DATE'], first_twelve['VALUE'])

plt.xticks(rotation = 45)       #指定角度

#print help(plt.xticks)

plt.show()


plt.plot(first_twelve['DATE'], first_twelve['VALUE'])

plt.xticks(rotation=90)#

plt.xlabel('Month')

plt.ylabel('Unemployment Rate')

plt.title('Monthly Unemployment Trends,1948')

plt.show()


import matplotlib.pyplot as plt

fig = plt.figure()    #指定一个默认画图的区间

ax1 = fig.add_subplot(4,3,1)

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

ax3 = fig.add_subplot(4,3,6)

plt.show()



import numpy as np

fig = plt.figure(figsize=(4,5))     #表示当前画图域的长4,宽为5

ax1 = fig.add_subplot(2,1,1)      #已经分别得到了ax1和ax2

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

ax1.plot(np.random.randint(1,5,5), np.arange(5)) #对ax1和ax2分别进行画图操作

ax2.plot(np.arange(10)*3, np.arange(10))

plt.show()



unrate['MONTH'] = unrate['DATE'].dt.month

unrate['MONTH'] = unrate['DATE'].dt.month

fig = plt.figure(figsize=(6,3))


plt.plot(unrate[0:12]['MONTH'], unrate[0:12]['VALUE'], c='red')

plt.plot(unrate[12:24]['MONTH'], unrate[12:24]['VALUE'], c ='blue')

plt.show()



fig = plt.figure(figsize=(10,6))

colors = ['red', 'blue', 'green', 'orange', 'black']

for i in range(5):

     start_index = i*12

    end_index = (i+1)*12

    subset = unrate[start_index:end_index]

    label = str(1948 + i)

    plt.plot(subset['MONTH'], subset['VALUE'], c=colors[i], label = label)

plt.legend(loc='best')

plt.show()



import pandas as pd

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]

print(norm_reviews[:1])

                             FILM  RT_user_norm  Metacritic_user_nom  \
0  Avengers: Age of Ultron (2015)           4.3                 3.55   

   IMDB_norm  Fandango_Ratingvalue  Fandango_Stars  
0        3.9                   4.5             5.0 



import matplotlib.pyplot as plt

from numpy import arange


num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']


bar_heights = norm_reviews.ix[0, num_cols].values   #表示当前这个柱的高为多少

print(bar_heights)

bar_positions = arange(5) +0.95

print(bar_positions)

fig, ax = plt.subplots()

ax.bar(bar_positions, bar_heights, 0.2)      #表示当前柱形图的宽度     

ax.set_xlabel('Rating Source')

ax.set_ylabel('Average Rating')

ax.set_title('Average User Rating For Avengers: Age of Ultron (2015)')

plt.show()


[4.2999999999999998 3.5499999999999998 3.8999999999999999 4.5 5.0]
[ 0.95  1.95  2.95  3.95  4.95]


import matplotlib.pyplot as plt

from numpy import arange

num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']


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

bar_positions = arange(5) +0.75

tick_positions = range(1,6)

fig, ax = plt.subplots()

ax.barh(bar_positions, bar_widths, 0.5)


ax.set_yticks(tick_positions)

ax.set_yticklabels(num_cols)

ax.set_ylabel('Rating Source')

ax.set_xlabel('Average Rating')

ax.set_title('Average User Rating For Avengers: Age of Ultron (2015)')

plt.show()




fig, 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 = plt.figure(figsize=(5,10))

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

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

ax1.scatter(norm_reviews['Fandango_Ratingvalue'], norm_reviews['RT_user_norm'])

ax1.set_xlabel('Fandango')

ax1.set_ylabel('Rotten Tomatoes')

ax2.scatter(norm_reviews['RT_user_norm'], norm_reviews['Fandango_Ratingvalue'])

ax2.set_xlabel('Rotten Tomatoes')

ax2.set_ylabel('Fandango')

plt.show()




fig, ax = plt.subplots()

#ax.hist(norm_reviews['Fandango_Ratingvalue'])                                     #默认画格

#ax.hist(norm_reviews['Fandango_Ratingvalue'], bins=20)                     #可以帮我们划分二十个格

ax.hist(norm_reviews['Fandango_Ratingvalue'], range=(4,5),bins=20)      #粗度为20


plt.show()



fig, ax = plt.subplots()

ax.boxplot(norm_reviews['RT_user_norm'])   #找这一列

ax.set_xticklabels(['Rotten Tomatoes'])        #设置底标

ax.set_ylim(0,5)                               

plt.show()




num_cols =['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue']   #

fig, ax = plt.subplots()

ax.boxplot(norm_reviews[num_cols].values)

ax.set_xticklabels(num_cols,rotation=90)
ax.set_ylim(0,5)
plt.show()










 
 
 
 











































猜你喜欢

转载自blog.csdn.net/qq_41858768/article/details/79615516