14天数据分析与机器学习实践之Day03——可视化库matpltlib应用总结

14天数据分析与机器学习实践之Day03——可视化库matpltlib应用总结

折线图绘制

基本的图
创建画布并展示

import matplotlib.pyplot as plt
plt.plot()#创建画布
plt.show()#展示

在这里插入图片描述
取出前12个数据
以DATE为x轴以VALUE为y轴

first_twelve = unrate[0:12]
plt.plot(first_twelve['DATE'], first_twelve['VALUE'])
plt.show()

在这里插入图片描述
将坐标轴调斜
横轴旋转45°

plt.plot(first_twelve['DATE'], first_twelve['VALUE'])
plt.xticks(rotation=45)
#print help(plt.xticks)
plt.show()

在这里插入图片描述
将横轴纵着写(旋转90°)
并且添加x,y轴的名字和标题

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()

在这里插入图片描述

子图操作

plt.figure()找到画图域
fig.add_subplot(3,2,1)表示一个三行二列的矩阵区间
最后一个数字表示第n个子图

import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(3,2,1)
ax2 = fig.add_subplot(3,2,2)
ax2 = fig.add_subplot(3,2,6)
plt.show()

在这里插入图片描述
指定fig = plt.figure(figsize=(3, 3))画图区域为3*3

import numpy as np
fig = plt.figure()
#fig = plt.figure(figsize=(3, 3))
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)

ax1.plot(np.random.randint(1,5,5), np.arange(5))
ax2.plot(np.arange(10)*3, np.arange(10))
plt.show()

绘画两条曲线,在最后添加c='color’

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')
#print help(plt.legend)
plt.show()

label = str(1948 + i)为当前线所指值
plt.legend(loc=‘best’)显示label方框在随机位置
plt.legend(loc=‘upper right’)显示label方框右上角
plt.legend(loc=‘upper left’)显示label方框左上角
low right、low left、center left、center right都可作为参数
在这里插入图片描述

条形图与散点图

bar_heights = norm_reviews.ix[0, num_cols].values
#柱高
bar_positions = arange(5) + 0.75
#柱间距离
ax.bar(bar_positions, bar_heights, 0.5)
#柱宽度0.5

import matplotlib.pyplot as plt
from numpy import arange
#The Axes.bar() method has 2 required parameters, left and height. 
#We use the left parameter to specify the x coordinates of the left sides of the bar. 
#We use the height parameter to specify the height of each bar
#表示列
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.75
#print bar_positions#柱间距离
fig, ax = plt.subplots()
ax.bar(bar_positions, bar_heights, 0.5)
#柱宽度0.5
plt.show()

在这里插入图片描述

散点图

ax.scatter画出点

#Let's look at a plot that can help us visualize many points.
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, 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)
plt.show()

使用bins设置区间
ax1.set_ylim(0, 50)设置y区间

绘制盒图

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()

在这里插入图片描述

细节设置

去掉轴上的标度
ax.tick_params(bottom=“off”, top=“off”, left=“off”, right=“off”)
设置颜色

ax.plot(women_degrees['Year'], women_degrees[major_cats[sp]], c='blue', label='Women')
ax.plot(women_degrees['Year'], 100-women_degrees[major_cats[sp]], c='green', label='Men')
cb_dark_blue = (0/255, 107/255, 164/255)
cb_orange = (255/255, 128/255, 14/255)
ax.plot(women_degrees['Year'], women_degrees[stem_cats[sp]], c=cb_dark_blue, label='Women', linewidth=3)

设置线粗

ax.plot(women_degrees['Year'], women_degrees[major_cats[sp]], c=cb_dark_blue, label='Women', linewidth=10)
ax.plot(women_degrees['Year'], 100-women_degrees[major_cats[sp]], c=cb_orange, label='Men', linewidth=10)

添加文字

    if sp == 0:
        ax.text(2005, 87, 'Men')
        ax.text(2002, 8, 'Women')
    elif sp == 5:
        ax.text(2005, 62, 'Men')
        ax.text(2001, 35, 'Women')

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值