matpltlib

可视化图

表格说明:1948到2016年,当年失业人数占总人数的百分比,最小单位是月

折线图

1984年的走势

# pandas加载数据,数据中有日期转为日期格式
import pandas as pd
unrate = pd.read_csv('unrate.csv')
unrate['DATE'] = pd.to_datetime(unrate['DATE'])
'''
DATE      object
VALUE    float64
dtype: object
DATE     datetime64[ns]
VALUE           float64
dtype: object
'''

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

# 改变横坐标下标的角度
plt.xticks(rotation=45)

# 添加x,y以及标题的文字说明
plt.xlabel('Month')
plt.ylabel('Unemployment Rate')
plt.title('Monthly Unemployment Trends, 1948')
# 有的时候要显示中文,就要重新设置一下了

为啥我的列表是12个,显示在图上只有偶数的坐标?

醉了,参考https://blog.csdn.net/Poul_henry/article/details/82590392

xticks()和yticks(),修改刻度

import matplotlib.dates as mdate
first_twelve = unrate[0:12]
# 设置画布的大小
fig1 = plt.figure(figsize=(15,5))
#添加子图
ax1 = fig1.add_subplot(1,1,1)
# 设置显示日期的格式
ax1.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d'))
# 添加这句按照x轴的内容显示
plt.xticks(first_twelve['DATE'])
plt.plot(first_twelve['DATE'], first_twelve['VALUE'],'o-')
plt.show()

还有一个方法是自己设置一个时间间隔,也是在plt.xticks里修改

https://blog.csdn.net/zenghaihong/article/details/70747247

https://blog.csdn.net/kancy110/article/details/77131539

当当当~

显示好了

子图

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,3)
ax2 = fig.add_subplot(3,2,5)
plt.show()

3行2列

分别画两个图,在两个子图上画

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),'o-')
ax2.plot(np.arange(10)*3, np.arange(10),'o-')
plt.show()

两条线画在一个图中,在一个图里面画,写两遍用不同的颜色表示

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]
    plt.plot(subset['MONTH'], subset['VALUE'], c=colors[i])
    
plt.show()

在图上加上label,加上标签

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)
# 显示label
plt.legend(loc='upper left')
plt.xlabel('Month, Integer')
plt.ylabel('Unemployment Rate, Percent')
plt.title('Monthly Unemployment Trends, 1948-1952')

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

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
# X轴的间隔
bar_positions = arange(5) + 0.75
fig, ax = plt.subplots()
# 0.5是矩形图的宽
ax.bar(bar_positions, bar_heights, 0.5)
plt.show()

# 横着画条形图
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()

柱形图

查看某一列的数值统计情况,排序查看,为后面画柱形图做准备

fandango_distribution = norm_reviews['Fandango_Ratingvalue'].value_counts()
fandango_distribution = fandango_distribution.sort_index()
print(fandango_distribution)
'''
2.7     2
2.8     2
2.9     5
3.0     4
3.1     3
3.2     5
3.3     4
3.4     9
3.5     9
3.6     8
3.7     9
3.8     5
3.9    12
4.0     7
4.1    16
4.2    12
4.3    11
4.4     7
4.5     9
4.6     4
4.8     3
Name: Fandango_Ratingvalue, dtype: int64
'''

画图

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

盒图

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

数据:不同学科随着年龄的增长,男女比例

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值