AI day04(2020 8/3)

Matplotlib

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# 常见的无衬线字体有 Trebuchet MS, Tahoma, Verdana, Arial, Helvetica,SimHei 中文的幼圆、隶书等等
plt.rcParams['font.sans-serif'] = ['FangSong']  # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题
data = pd.read_csv('data/unrate.csv')
data
DATEVALUE
01948-01-013.4
11948-02-013.8
21948-03-014.0
31948-04-013.9
41948-05-013.5
51948-06-013.6
61948-07-013.6
71948-08-013.9
81948-09-013.8
91948-10-013.7
101948-11-013.8
111948-12-014.0
121949-01-014.3
131949-02-014.7
141949-03-015.0
151949-04-015.3
161949-05-016.1
171949-06-016.2
181949-07-016.7
191949-08-016.8
201949-09-016.6
211949-10-017.9
221949-11-016.4
231949-12-016.6
241950-01-016.5
251950-02-016.4
261950-03-016.3
271950-04-015.8
281950-05-015.5
291950-06-015.4
.........
7942014-03-016.7
7952014-04-016.2
7962014-05-016.2
7972014-06-016.1
7982014-07-016.2
7992014-08-016.2
8002014-09-016.0
8012014-10-015.7
8022014-11-015.8
8032014-12-015.6
8042015-01-015.7
8052015-02-015.5
8062015-03-015.5
8072015-04-015.4
8082015-05-015.5
8092015-06-015.3
8102015-07-015.3
8112015-08-015.1
8122015-09-015.1
8132015-10-015.0
8142015-11-015.0
8152015-12-015.0
8162016-01-014.9
8172016-02-014.9
8182016-03-015.0
8192016-04-015.0
8202016-05-014.7
8212016-06-014.9
8222016-07-014.9
8232016-08-014.9

824 rows × 2 columns

data.head()
DATEVALUE
01948-01-013.4
11948-02-013.8
21948-03-014.0
31948-04-013.9
41948-05-013.5
unrate = data.copy()
unrate['DATE'] = pd.to_datetime(unrate['DATE'])
unrate.head(12)
DATEVALUE
01948-01-013.4
11948-02-013.8
21948-03-014.0
31948-04-013.9
41948-05-013.5
51948-06-013.6
61948-07-013.6
71948-08-013.9
81948-09-013.8
91948-10-013.7
101948-11-013.8
111948-12-014.0
plt.plot()
plt.show()
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/font_manager.py:1331: UserWarning: findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans
  (prop.get_family(), self.defaultFamily[fontext]))

在这里插入图片描述

first_twelve = unrate[0:12]
first_twelve
DATEVALUE
01948-01-013.4
11948-02-013.8
21948-03-014.0
31948-04-013.9
41948-05-013.5
51948-06-013.6
61948-07-013.6
71948-08-013.9
81948-09-013.8
91948-10-013.7
101948-11-013.8
111948-12-014.0
# x,y 默认是折线图
plt.plot(first_twelve['DATE'],first_twelve['VALUE'])
plt.show()

在这里插入图片描述

plt.plot(first_twelve['DATE'],first_twelve['VALUE'])
# 设置对应轴的角度
plt.xticks(rotation = 45)
plt.show()

在这里插入图片描述

plt.plot(first_twelve['DATE'],first_twelve['VALUE'])
# 设置对应轴的角度
plt.xticks(rotation = 45)
# 设置x轴的标签
plt.xlabel('month')
# 设置y轴的标签
plt.ylabel('value')
# 设置图的标题
plt.title('unrate')
plt.show()

在这里插入图片描述

# figsize=(10,10) 设置大小为10*10
fig = plt.figure(figsize=(10,10))
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,4)
ax1.plot(np.random.randint(1,5,5),np.arange(5))
ax2.plot(np.arange(10)*3,np.arange(10))
plt.show()

在这里插入图片描述

unrate['MONTH'] = unrate['DATE'].dt.month
print(unrate['MONTH'])
0       1
1       2
2       3
3       4
4       5
5       6
6       7
7       8
8       9
9      10
10     11
11     12
12      1
13      2
14      3
15      4
16      5
17      6
18      7
19      8
20      9
21     10
22     11
23     12
24      1
25      2
26      3
27      4
28      5
29      6
       ..
794     3
795     4
796     5
797     6
798     7
799     8
800     9
801    10
802    11
803    12
804     1
805     2
806     3
807     4
808     5
809     6
810     7
811     8
812     9
813    10
814    11
815    12
816     1
817     2
818     3
819     4
820     5
821     6
822     7
823     8
Name: MONTH, Length: 824, dtype: int64
# 同一图表画多条线
fig = plt.figure(figsize=(5,5))
plt.plot(unrate[0:12]['MONTH'],unrate[0:12]['VALUE'],c='red',label='1948')
plt.plot(unrate[12:24]['MONTH'],unrate[12:24]['VALUE'],c='green',label='1949')
plt.legend(loc='best')
plt.show()

在这里插入图片描述

reviews = pd.read_csv('data/fandango_score_comparison.csv')
reviews
FILMRottenTomatoesRottenTomatoes_UserMetacriticMetacritic_UserIMDBFandango_StarsFandango_RatingvalueRT_normRT_user_norm...IMDB_normRT_norm_roundRT_user_norm_roundMetacritic_norm_roundMetacritic_user_norm_roundIMDB_norm_roundMetacritic_user_vote_countIMDB_user_vote_countFandango_votesFandango_Difference
0Avengers: Age of Ultron (2015)7486667.17.85.04.53.704.30...3.903.54.53.53.54.01330271107148460.5
1Cinderella (2015)8580677.57.15.04.54.254.00...3.554.54.03.54.03.524965709126400.5
2Ant-Man (2015)8090648.17.85.04.54.004.50...3.904.04.53.04.04.0627103660120550.5
3Do You Believe? (2015)1884224.75.45.04.50.904.20...2.701.04.01.02.52.531313617930.5
4Hot Tub Time Machine 2 (2015)1428293.45.13.53.00.701.40...2.550.51.51.51.52.5881956010210.5
5The Water Diviner (2015)6362506.87.24.54.03.153.10...3.603.03.02.53.53.534393733970.5
6Irrational Man (2015)4253537.66.94.03.52.102.65...3.452.02.52.54.03.51726802520.5
7Top Five (2014)8664816.86.54.03.54.303.20...3.254.53.04.03.53.51241687632230.5
8Shaun the Sheep Movie (2015)9982818.87.44.54.04.954.10...3.705.04.04.04.53.562122278960.5
9Love & Mercy (2015)8987808.57.84.54.04.454.35...3.904.54.54.04.54.05453678640.5
10Far From The Madding Crowd (2015)8477717.57.24.54.04.203.85...3.604.04.03.54.03.535121298040.5
11Black Sea (2015)8260626.66.44.03.54.103.00...3.204.03.03.03.53.037165472180.5
12Leviathan (2014)9979927.27.74.03.54.953.95...3.855.04.04.53.54.014522521640.5
13Unbroken (2014)5170596.57.24.54.12.553.50...3.602.53.53.03.53.52187751894430.4
14The Imitation Game (2014)9092738.28.15.04.64.504.60...4.054.54.53.54.04.056633416480550.4
15Taken 3 (2015)946264.66.14.54.10.452.30...3.050.52.51.52.53.024010423567570.4
16Ted 2 (2015)4658486.56.64.54.12.302.90...3.302.53.02.53.53.51974910264370.4
17Southpaw (2015)5980578.27.85.04.62.954.00...3.903.04.03.04.04.01282356155970.4
18Night at the Museum: Secret of the Tomb (2014)5058475.86.34.54.12.502.90...3.152.53.02.53.03.01035029154450.4
19Pixels (2015)1754275.35.64.54.10.852.70...2.801.02.51.52.53.02461952138860.4
20McFarland, USA (2015)7989607.27.55.04.63.954.45...3.754.04.53.03.54.0591376933640.4
21Insidious: Chapter 3 (2015)5956526.96.34.54.12.952.80...3.153.03.02.53.53.01152513432760.4
22The Man From U.N.C.L.E. (2015)6880557.97.64.54.13.404.00...3.803.54.03.04.04.01442210426860.4
23Run All Night (2015)6059597.36.64.54.13.002.95...3.303.03.03.03.53.51415043820660.4
24Trainwreck (2015)8574756.06.74.54.14.253.70...3.354.53.54.03.03.51692738083810.4
25Selma (2014)9986897.17.55.04.64.954.30...3.755.04.54.53.54.03164534470250.4
26Ex Machina (2015)9286787.97.74.54.14.604.30...3.854.54.54.04.04.067215449934580.4
27Still Alice (2015)8885727.87.54.54.14.404.25...3.754.54.53.54.04.01535712312580.4
28Wild Tales (2014)9692778.88.24.54.14.804.60...4.105.04.54.04.54.0107502852350.4
29The End of the Tour (2015)9289847.57.94.54.14.604.45...3.954.54.54.04.04.01913201210.4
..................................................................
116Clouds of Sils Maria (2015)8967787.16.83.53.44.453.35...3.404.53.54.03.53.536113921620.1
117Testament of Youth (2015)8179777.97.34.03.94.053.95...3.654.04.04.04.03.51554951270.1
118Infinitely Polar Bear (2015)8076647.97.24.03.94.003.80...3.604.04.03.04.03.5810621240.1
119Phoenix (2015)9981918.07.23.53.44.954.05...3.605.04.04.54.03.5213687700.1
120The Wolfpack (2015)8473757.07.13.53.44.203.65...3.554.03.54.03.53.581488660.1
121The Stanford Prison Experiment (2015)8487688.57.14.03.94.204.35...3.554.04.53.54.53.56950510.1
122Tangerine (2015)9586867.37.44.03.94.754.30...3.705.04.54.53.53.514696360.1
123Magic Mike XXL (2015)6264605.46.34.54.43.103.20...3.153.03.03.02.53.0521193793630.1
124Home (2015)4565557.36.74.54.42.253.25...3.352.53.53.03.53.51774115877050.1
125The Wedding Ringer (2015)2766353.36.74.54.41.353.30...3.351.53.52.01.53.51263729265060.1
126Woman in Gold (2015)5281517.27.44.54.42.604.05...3.702.54.02.53.53.5721795724350.1
127The Last Five Years (2015)6060606.96.04.54.43.003.00...3.003.03.03.03.53.0204110990.1
128Mission: Impossible – Rogue Nation (2015)9290758.07.84.54.44.604.50...3.904.54.54.04.04.03628257983570.1
129Amy (2015)9791858.88.04.54.44.854.55...4.005.04.54.54.54.06056307290.1
130Jurassic World (2015)7181597.07.34.54.53.554.05...3.653.54.03.03.53.51281241807343900.0
131Minions (2015)5452565.76.74.04.02.702.60...3.352.52.53.03.03.520455895149980.0
132Max (2015)3573475.97.04.54.51.753.65...3.502.03.52.53.03.515544434120.0
133Paul Blart: Mall Cop 2 (2015)536132.44.33.53.50.251.80...2.150.52.00.51.02.02111500430540.0
134The Longest Ride (2015)3173334.87.24.54.51.553.65...3.601.53.51.52.53.5492521426030.0
135The Lazarus Effect (2015)1423314.95.23.03.00.701.15...2.600.51.01.52.52.5621769116510.0
136The Woman In Black 2 Angel of Death (2015)2225424.44.93.03.01.101.25...2.451.01.52.02.02.5551487313330.0
137Danny Collins (2015)7775587.17.14.04.03.853.75...3.554.04.03.03.53.533112065310.0
138Spare Parts (2015)5283507.17.24.54.52.604.15...3.602.54.02.53.53.57473774500.0
139Serena (2015)1825365.35.43.03.00.901.25...2.701.01.52.02.52.51912165500.0
140Inside Out (2015)9890948.98.64.54.54.904.50...4.305.04.54.54.54.580796252157490.0
141Mr. Holmes (2015)8778677.97.44.04.04.353.90...3.704.54.03.54.03.533736713480.0
142'71 (2015)9782837.57.23.53.54.854.10...3.605.04.04.04.03.560241161920.0
143Two Days, One Night (2014)9778898.87.43.53.54.853.90...3.705.04.04.54.53.5123243451180.0
144Gett: The Trial of Viviane Amsalem (2015)10081907.37.83.53.55.004.05...3.905.04.04.53.54.0191955590.0
145Kumiko, The Treasure Hunter (2015)8763686.46.73.53.54.353.15...3.354.53.03.53.03.5195289410.0

146 rows × 22 columns

cols = ['FILM','RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']
norm_reviews = reviews[cols]
norm_reviews
FILMRT_user_normMetacritic_user_nomIMDB_normFandango_RatingvalueFandango_Stars
0Avengers: Age of Ultron (2015)4.303.553.904.55.0
1Cinderella (2015)4.003.753.554.55.0
2Ant-Man (2015)4.504.053.904.55.0
3Do You Believe? (2015)4.202.352.704.55.0
4Hot Tub Time Machine 2 (2015)1.401.702.553.03.5
5The Water Diviner (2015)3.103.403.604.04.5
6Irrational Man (2015)2.653.803.453.54.0
7Top Five (2014)3.203.403.253.54.0
8Shaun the Sheep Movie (2015)4.104.403.704.04.5
9Love & Mercy (2015)4.354.253.904.04.5
10Far From The Madding Crowd (2015)3.853.753.604.04.5
11Black Sea (2015)3.003.303.203.54.0
12Leviathan (2014)3.953.603.853.54.0
13Unbroken (2014)3.503.253.604.14.5
14The Imitation Game (2014)4.604.104.054.65.0
15Taken 3 (2015)2.302.303.054.14.5
16Ted 2 (2015)2.903.253.304.14.5
17Southpaw (2015)4.004.103.904.65.0
18Night at the Museum: Secret of the Tomb (2014)2.902.903.154.14.5
19Pixels (2015)2.702.652.804.14.5
20McFarland, USA (2015)4.453.603.754.65.0
21Insidious: Chapter 3 (2015)2.803.453.154.14.5
22The Man From U.N.C.L.E. (2015)4.003.953.804.14.5
23Run All Night (2015)2.953.653.304.14.5
24Trainwreck (2015)3.703.003.354.14.5
25Selma (2014)4.303.553.754.65.0
26Ex Machina (2015)4.303.953.854.14.5
27Still Alice (2015)4.253.903.754.14.5
28Wild Tales (2014)4.604.404.104.14.5
29The End of the Tour (2015)4.453.753.954.14.5
.....................
116Clouds of Sils Maria (2015)3.353.553.403.43.5
117Testament of Youth (2015)3.953.953.653.94.0
118Infinitely Polar Bear (2015)3.803.953.603.94.0
119Phoenix (2015)4.054.003.603.43.5
120The Wolfpack (2015)3.653.503.553.43.5
121The Stanford Prison Experiment (2015)4.354.253.553.94.0
122Tangerine (2015)4.303.653.703.94.0
123Magic Mike XXL (2015)3.202.703.154.44.5
124Home (2015)3.253.653.354.44.5
125The Wedding Ringer (2015)3.301.653.354.44.5
126Woman in Gold (2015)4.053.603.704.44.5
127The Last Five Years (2015)3.003.453.004.44.5
128Mission: Impossible – Rogue Nation (2015)4.504.003.904.44.5
129Amy (2015)4.554.404.004.44.5
130Jurassic World (2015)4.053.503.654.54.5
131Minions (2015)2.602.853.354.04.0
132Max (2015)3.652.953.504.54.5
133Paul Blart: Mall Cop 2 (2015)1.801.202.153.53.5
134The Longest Ride (2015)3.652.403.604.54.5
135The Lazarus Effect (2015)1.152.452.603.03.0
136The Woman In Black 2 Angel of Death (2015)1.252.202.453.03.0
137Danny Collins (2015)3.753.553.554.04.0
138Spare Parts (2015)4.153.553.604.54.5
139Serena (2015)1.252.652.703.03.0
140Inside Out (2015)4.504.454.304.54.5
141Mr. Holmes (2015)3.903.953.704.04.0
142'71 (2015)4.103.753.603.53.5
143Two Days, One Night (2014)3.904.403.703.53.5
144Gett: The Trial of Viviane Amsalem (2015)4.053.653.903.53.5
145Kumiko, The Treasure Hunter (2015)3.153.203.353.53.5

146 rows × 6 columns

norm_reviews[:1]
FILMRT_user_normMetacritic_user_nomIMDB_normFandango_RatingvalueFandango_Stars
0Avengers: Age of Ultron (2015)4.33.553.94.55.0
num_cols = ['RT_user_norm','Metacritic_user_nom','IMDB_norm','Fandango_Ratingvalue','Fandango_Stars']
bar_height = norm_reviews.loc[0,num_cols].values
bar_height
array([4.3, 3.55, 3.9, 4.5, 5.0], dtype=object)
bar_positions = np.arange(5) + 0.75
bar_positions
array([0.75, 1.75, 2.75, 3.75, 4.75])
fig,ax = plt.subplots()
# 0.3 为宽度占比
# bar 条形图
ax.bar(bar_positions,bar_height,0.3)
plt.show()

在这里插入图片描述

# 散点图
fig,ax = plt.subplots()
ax.scatter(norm_reviews['Fandango_Ratingvalue'],norm_reviews['RT_user_norm'])
ax.set_xlabel('Fandango_Ratingvalue')
ax.set_ylabel('RT_user_norm')
plt.show()

在这里插入图片描述

# 柱形图
#画图
fig,ax = plt.subplots()
#hist()表示带有bins结构,默认bins为10个。
#bins:某个变量过多,坐标轴就化不开,用bins化成范围,减少变量数量。
#ax.hist(norm_reviews['Fandango_Ratingvalue'])
#ax.hist(norm_reviews['Fandango_Ratingvalue'],bins=20) #指定bins为20个
#指定bins和指定横坐标区间
ax.hist(norm_reviews['Fandango_Ratingvalue'],range=(4,5),bins=20)
# set_ylim(x,y) 指定y轴的区间
plt.show()

在这里插入图片描述

# 箱型图 ----> 发布情况
fig,ax = plt.subplots()
ax.boxplot(norm_reviews['RT_user_norm'])
# 设置x轴的名称
ax.set_xticklabels(['Rotten'])
ax.set_ylim(0,5)
plt.show()

在这里插入图片描述

Seaborn

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mlp
import numpy as np
%matplotlib inline
def sinplot(flip=1):
    x = np.linspace(0,14,1000)
    for i in range(1,7):
        plt.plot(x,np.sin(x+i*0.5)*(7-i)*flip)

sinplot()
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/font_manager.py:1331: UserWarning: findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans
  (prop.get_family(), self.defaultFamily[fontext]))

在这里插入图片描述

# 使用seaborn模板
sns.set()
sinplot()

在这里插入图片描述

# 使用seaborn模板
"""
    五种风格
    darkgrid
    whitegrid
    dark
    white
    ticks
"""
sns.set_style('whitegrid')
# set_context('') 设置内容风格
sns.set_context('talk')
sinplot()

在这里插入图片描述

# 调色板
# set_palette() 设置所有图颜色
# color_palette() 设置图的颜色
# 默认颜色
current_palette = sns.color_palette()
print(current_palette)
sns.palplot(current_palette)
[(0.2980392156862745, 0.4470588235294118, 0.6901960784313725), (0.8666666666666667, 0.5176470588235295, 0.3215686274509804), (0.3333333333333333, 0.6588235294117647, 0.40784313725490196), (0.7686274509803922, 0.3058823529411765, 0.3215686274509804), (0.5058823529411764, 0.4470588235294118, 0.7019607843137254), (0.5764705882352941, 0.47058823529411764, 0.3764705882352941), (0.8549019607843137, 0.5450980392156862, 0.7647058823529411), (0.5490196078431373, 0.5490196078431373, 0.5490196078431373), (0.8, 0.7254901960784313, 0.4549019607843137), (0.39215686274509803, 0.7098039215686275, 0.803921568627451)]

在这里插入图片描述

# 第一个参数为颜色空间,第二个参数是返回的颜色个数
sns.palplot(sns.color_palette('hls',12))

在这里插入图片描述

# sns.hls_palette(8,l=0.7,s=0.5)
# l 表示亮度
# s 表示饱和度
sns.palplot(sns.hls_palette(8,l=0.7,s=0.5))

在这里插入图片描述



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值