matplotlib简单四图----条形图,点阵图,盒型图,箱形图

# -----------------------
# __Author : tyran
# __Date : 17-12-11
# -----------------------

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os

data_file_name = 'fandango_scores.csv'
data_file_path = os.path.join(os.getcwd(), 'Datas', data_file_name)
# 读取数据
data = pd.read_csv(filepath_or_buffer=data_file_path)
"""
第一个图开始,条形图
"""
# 查看的列
cols_ax1 = ['FILM', 'RottenTomatoes', 'RottenTomatoes_User', 'Metacritic']
# 获取符合cols_ax1的四列数据
score_view = data[cols_ax1]
# 提取第一行数据的指定列
film_score = score_view.ix[0, cols_ax1[1:]]
# print(film_score)
# 图表上每一列距离原点的距离
position = np.arange(3) + 0.5
# 设置画布尺寸
fig = plt.figure(figsize=[6, 8])
# 设置排列位置
ax_bar = fig.add_subplot(2, 2, 1)
# 设置数据
bar_items = ax_bar.bar(position, film_score, 0.3, color='r')
# 给每一个柱子打上值
for item in bar_items:
    height = item.get_height()
    ax_bar.text(item.get_x() + item.get_width() / 2,
                1.05 * height, height,
                ha='center', va='bottom')

ax_bar.set_xticklabels(score_view[cols_ax1[0]], rotation=15)
ax_bar.set_xticks(position)
"""
第二个图开始,点阵图
"""
cols_scatter = ['RottenTomatoes', 'RottenTomatoes_User']
scatter_data = data[cols_scatter]
s_data1, s_data2 = scatter_data['RottenTomatoes'], scatter_data['RottenTomatoes_User']
ax_scatter = fig.add_subplot(2, 2, 2)
ax_scatter.scatter(s_data1, s_data2)
ax_scatter.set_xlabel(s_data1.name)
ax_scatter.set_ylabel(s_data2.name)

"""
第三个图,柱形图 数据源是Fandango_Ratingvalue和IMDB
"""
cols_tmp = ['Fandango_Ratingvalue', 'IMDB']
# 这里就发现pandas很方便的地方了,可以通过value_counts,把Series按照值和值的出现次数重新组成Series
# 索引就是数值,值就是出现次数
# sort_index是让他按照索引排序
fandango_data, imdb_data = data[cols_tmp[0]].value_counts().sort_index(), \
                           data[cols_tmp[1]].value_counts().sort_index()
# print(fandango_data.max())
# print(imdb_data.max())
ax_hist = fig.add_subplot(2, 2, 3)
ax_hist.set_xticks(range(0, 17, 1))
# ec是颜色  ls是样式  lw是宽度  bins是把数据分组
ax_hist.hist(fandango_data, bins=10, color='g', ec='black', ls='--', label=fandango_data.name)
ax_hist.hist(imdb_data, bins=20, color='y', ec='black', ls='--', label=imdb_data.name)
# 显示label
plt.legend(loc='best')

"""
第四个图,盒形图
"""
# 先拿数据
box_cols = ['RT_norm', 'RT_user_norm']
ax_box = fig.add_subplot(2, 2, 4)
print(data[box_cols].columns)
box = ax_box.boxplot(data[box_cols].values, labels=data[box_cols].columns,
                     showmeans=True, )
plt.show()

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值