个人学习笔记,可能存在问题


文章目录

  • 1、 绘制折线图
  • 1.1 基本的图
  • 1.2 绘制折线
  • 1.3 对x轴文字进行调整
  • 1.4 对x、y、标题取名
  • 2、子图操作
  • 2.1 划分子图
  • 2.2 画布大小
  • 2.3 绘制不同线条颜色
  • 2.4 对不同颜色线条标签
  • 3、 条形图和散点图
  • 3.1 绘制条形图ax.bar
  • 3.2 条形图加x、y、标题
  • 3.3 绘制散点图ax.scatter
  • 4、 柱形图与盒图
  • 4.1 绘制柱形图ax.hist
  • 4.2 多个柱形图
  • 4.3 盒图
  • 5、 细节补充
  • 5.1 去掉轴上的标度
  • 5.2 设置线条颜色,粗细


1、 绘制折线图

1.1 基本的图

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

plot results在哪_plot results在哪

1.2 绘制折线

import pandas as pd
unrate = pd.read_csv('unrate.csv')
unrate['DATE'] = pd.to_datetime(unrate['DATE'])#改成datetime形式
print(unrate.head(12))#打印前12个数据

first_twelve = unrate[0:12]#选取前12列
plt.plot(first_twelve['DATE'], first_twelve['VALUE'])#x轴为data数据,y轴是value
plt.show()

plot results在哪_子图_02

1.3 对x轴文字进行调整

plt.plot(first_twelve['DATE'], first_twelve['VALUE'])
plt.xticks(rotation=45)#x轴文字旋转45度
plt.show()

plot results在哪_Matpltlib_03

1.4 对x、y、标题取名

plt.plot(first_twelve['DATE'], first_twelve['VALUE'])
plt.xticks(rotation=90)#x轴旋转90度
plt.xlabel('Month')#x轴名
plt.ylabel('Unemployment Rate')#y轴名
plt.title('Monthly Unemployment Trends, 1948')#标题名
plt.show()

plot results在哪_Matpltlib_04

2、子图操作

2.1 划分子图

import matplotlib.pyplot as plt
fig = plt.figure()#新建一个画布
ax1 = fig.add_subplot(3,2,1)#3行2列,在一号位置创建1个图
ax2 = fig.add_subplot(3,2,2)
ax2 = fig.add_subplot(3,2,6)
plt.show()

plot results在哪_子图_05

2.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)

2.3 绘制不同线条颜色

unrate['MONTH'] = unrate['DATE'].dt.month
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()

plot results在哪_子图_06

2.4 对不同颜色线条标签

在plot中设置label,代表标签的解释

plt.legend(loc=‘best’) loc=best 系统自己定义最佳位置

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

plot results在哪_折线_07


如果 loc =upper left 则标签放左侧

plot results在哪_机器学习_08

3、 条形图和散点图

3.1 绘制条形图ax.bar

关于ix用法点击这里

用ax.hbar代表横向条形图

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#条形高度
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()

plot results在哪_机器学习_09

3.2 条形图加x、y、标题

num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']
bar_heights = norm_reviews.ix[0, num_cols].values
bar_positions = arange(5) + 0.75
tick_positions = range(1,6)
fig, ax = plt.subplots()

ax.bar(bar_positions, bar_heights, 0.5)
ax.set_xticks(tick_positions)
ax.set_xticklabels(num_cols, rotation=45)

ax.set_xlabel('Rating Source')
ax.set_ylabel('Average Rating')
ax.set_title('Average User Rating For Avengers: Age of Ultron (2015)')
plt.show()

3.3 绘制散点图ax.scatter

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

plot results在哪_Matpltlib_10

4、 柱形图与盒图

4.1 绘制柱形图ax.hist

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)
#只显示4-5数值,bins是将x轴分成20份
plt.show()

plot results在哪_Matpltlib_11

4.2 多个柱形图

ax1.set_ylim 在y轴限制长度

fig = plt.figure(figsize=(5,10))
ax1 = fig.add_subplot(4,1,1)
ax2 = fig.add_subplot(4,1,2)
ax3 = fig.add_subplot(4,1,3)
ax4 = fig.add_subplot(4,1,4)
ax1.hist(norm_reviews['Fandango_Ratingvalue'], bins=20, range=(0, 5))
ax1.set_title('Distribution of Fandango Ratings')
ax1.set_ylim(0, 50)

ax2.hist(norm_reviews['RT_user_norm'], 20, range=(0, 5))
ax2.set_title('Distribution of Rotten Tomatoes Ratings')
ax2.set_ylim(0, 50)

ax3.hist(norm_reviews['Metacritic_user_nom'], 20, range=(0, 5))
ax3.set_title('Distribution of Metacritic Ratings')
ax3.set_ylim(0, 50)

ax4.hist(norm_reviews['IMDB_norm'], 20, range=(0, 5))
ax4.set_title('Distribution of IMDB Ratings')
ax4.set_ylim(0, 50)

plt.show()

plot results在哪_机器学习_12

4.3 盒图

fig, ax = plt.subplots()
ax.boxplot(norm_reviews['RT_user_norm'])
ax.set_xticklabels(['Rotten Tomatoes'])
ax.set_ylim(0, 5)
plt.show()

plot results在哪_机器学习_13

5、 细节补充

5.1 去掉轴上的标度

plot results在哪_机器学习_14

ax.tick_params(bottom="off", top="off", left="off", right="off")
即可

5.2 设置线条颜色,粗细

粗细是通过linewidth设置

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)