美文网首页
三、matplotlib

三、matplotlib

作者: 一闪一闪亮日日日日日日 | 来源:发表于2018-10-08 21:24 被阅读0次

1 折线图

import pandas
import matplotlib.pylab as plt
sentiment = pandas.read_csv('sentiment.csv')
print(sentiment.columns)
sentiment['DATE'] = pandas.to_datetime(sentiment['DATE']) #转为标准时间模式
print(sentiment.head(5))
first_12 = sentiment[0:12]
plt.plot(first_12['DATE'],first_12['UMCSENT']) #绘制折线图
plt.xticks(rotation=45) #x轴斜着写
plt.xlabel('month') #x标签
plt.ylabel('sentiment') #y标签
plt.title('Monthly Sentiment Trend,2000') #标题
plt.show() #图片显示

输出:
Index(['DATE', 'UMCSENT'], dtype='object')
DATE UMCSENT
0 2000-01-01 112.0
1 2000-02-01 111.3
2 2000-03-01 107.1
3 2000-04-01 109.2
4 2000-05-01 110.7


Figure_1.png

2 子图设置

2.1 add_subplot()

import matplotlib.pylab as plt
import numpy
fig = plt.figure(figsize=(6,6))  #画图域
ax1 = fig.add_subplot(2,3,1)
ax2 = fig.add_subplot(2,3,2)
ax3 = fig.add_subplot(2,3,6)
ax1.plot(numpy.random.randint(1,5,5),numpy.arange(5))
ax2.plot(numpy.arange(10)*3,numpy.arange(10))
plt.show()

输出:


Figure_2.png

2.2 同张图画两条

import pandas
sentiment = pandas.read_csv('sentiment.csv')
print(sentiment.columns)
sentiment['DATE'] = pandas.to_datetime(sentiment['DATE'])
sentiment['MONTH'] = sentiment['DATE'].dt.month #取月份
sentiment['MONTH'] = sentiment['DATE'].dt.month
fig = plt.figure(figsize=(12,6))
plt.plot(sentiment[0:12]['MONTH'],sentiment[0:12]['UMCSENT'],c='red')
plt.plot(sentiment[12:24]['MONTH'],sentiment[12:24]['UMCSENT'],c='blue')
plt.xticks(rotation=45)
plt.xlabel('month')
plt.show()

输出:


Figure_3.png

2.3 用for循环画图,并设置标签

import matplotlib.pylab as plt
import pandas
sentiment = pandas.read_csv('sentiment.csv')
sentiment['DATE'] = pandas.to_datetime(sentiment['DATE'])
fig = plt.figure(figsize=(12,6))
colours = ['red','blue','green','orange','pink']
for i in range(5):
    star_index = i*12
    end_index = (i+1)*12
    y_i = sentiment[star_index:end_index]
    label = str(2000+i)
    plt.plot(y_i['DATE'].dt.month,y_i['UMCSENT'],c=colours[i],linewidth = 3,label=label)#linewidth指定粗细
    # plt.text(12,90,'2003') #添加文字到图上
plt.legend(loc=0) #加标签 1-右上角,2-左上角,3-左下角,4-右下角,0-最合适的位置
# print(help(plt.legend))
plt.show()

输出:


Figure_4.png

3 柱状图和条形图

import pandas
import matplotlib.pylab as plt
from numpy import arange
nba_2013 = pandas.read_csv('nba_2013.csv')
print(nba_2013.columns)
num_columns = ['age','g','gs','fg','fga']
bar_hights = nba_2013.ix[0,num_columns].values
print(bar_hights)
bar_positions = arange(5)+0.75
print(bar_positions)
#柱状图
ax=plt.subplot(121)
ax.bar(bar_positions,bar_hights,0.3) #0.3:柱的宽度  bar柱形图
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_xticklabels(num_columns)
#条形图
ax2=plt.subplot(122)
ax2.barh(bar_positions,bar_hights,0.3) #0.3:柱的宽度  barh条形图
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_xticklabels(num_columns)
plt.show()

输出:
Index(['player', 'pos', 'age', 'bref_team_id', 'g', 'gs', 'mp', 'fg', 'fga',
'fg.', 'x3p', 'x3pa', 'x3p.', 'x2p', 'x2pa', 'x2p.', 'efg.', 'ft',
'fta', 'ft.', 'orb', 'drb', 'trb', 'ast', 'stl', 'blk', 'tov', 'pf',
'pts', 'season', 'season_end'],
dtype='object')
[23 63 0 66 141]
[0.75 1.75 2.75 3.75 4.75]


Figure_5.png

4 散点图

ax = plt.subplot()
ax.scatter(nba_2013['age'],nba_2013['g'])
ax.set_xlabel('age')
ax.set_ylabel('g')
plt.show()

输出:


Figure_6.png

5 直方图

import pandas as pd
import matplotlib.pylab as plt
nba_2013 = pd.read_csv('nba_2013.csv')
cols = ['age','g','gs','fg','fga']
nba = nba_2013[cols]
print(nba[:5])
Age = nba['age'].value_counts() #计算相同年龄的个数
Age = Age.sort_index()
G = nba['g'].value_counts()
G = G.sort_index()
print('----------------')
print(Age)
print('-------------------')
print(G)
ax = plt.subplot()
ax.hist(nba_2013['age'],bins=20) #20个柱形
ax.ylim(0.90)  #设置y轴区间
plt.show()

输出:

   age   g  gs   fg   fga
0   23  63   0   66   141
1   20  81  20   93   185
2   27  53  12  143   275
3   28  73  73  464  1011
4   25  56  30  136   249
----------------
19     2
20    16
21    23
22    41
23    50
24    45
25    51
26    43
27    39
28    36
29    29
30    17
31    21
32    14
33    20
34     8
35    10
36     6
37     6
38     2
39     2
Name: age, dtype: int64
-------------------
1      2
2     11
3      6
4      2
5      6
6      5
7      2
8      4
9      3
10     1
11     5
12     1
13     3
14     4
15     5
17     1
18     1
19     3
20     6
21     5
22     8
23     5
24     6
25     2
26     5
27     2
28     2
29     4
30     3
31     7
      ..
54     8
55     8
56     4
57     1
58     5
59     4
60     6
61     6
62     7
63     8
64     7
65     7
66     4
67     5
68     9
69     7
70     9
71     9
72    13
73    18
74     8
75     3
76    10
77    14
78    12
79    14
80    23
81    22
82    28
83     1
Name: g, Length: 82, dtype: int64
Figure_7.png

6 箱线图

6.1 一个

nba_2013 = pd.read_csv('nba_2013.csv')
ax = plt.subplot()
ax.boxplot(nba_2013['age']) #.values 也行
ax.set_xticklabels(['age'])
plt.show()

输出:


Figure_8.png

6.2 多个

nba_2013 = pd.read_csv('nba_2013.csv')
nba_col = ['age','g','gs','fg','fga']
nba = nba_2013[nba_col]
ax = plt.subplot()
ax.boxplot(nba.values)
ax.set_xticklabels(nba_col,rotation=45)
plt.show()

输出:


Figure_9.png

相关文章

网友评论

      本文标题:三、matplotlib

      本文链接:https://www.haomeiwen.com/subject/gqajaftx.html