Python数据分析-数据可视化


matlab绘图

在matlab中,使用基本的绘图函数可以绘制出各种曲线图。matlab绘图简单易用,容易上手,能快速绘制出漂亮的图片。

matlab绘图基础:MATLAB 绘图 | 易安的小窝

matplotlib

matplotlib是受MATLAB的启发构建的。调用方法基本相同。

导入包

import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rc("font",family='times new roman')

若图片中有中文字体,需设置为中文

matplotlib.rc("font",family='KaiTi')

绘图时需要使用plt.show()显示出图像

可以通过设置%matplotlib inline默认显示图像

%matplotlib inline

1. 基本二维绘图

1.1 绘制折线图

plt.figure(figsize=[3, 3])  # 设置图窗大小
plt.plot([1,3,6,7,12])  # 绘图

outputs:

[<matplotlib.lines.Line2D at 0x2579751a430>]

png

1.2 绘制散点图

plt.figure(figsize=[4, 4])
plt.plot([1,3,5,7,9], [1,3,6,7,12], 'ro')

outputs:

[<matplotlib.lines.Line2D at 0x2579736a310>]

png

其中plt.plot([1,3,5,7,9], [1,3,6,7,12], 'ro')中三个参数:

  • [1,3,5,7,9] 横坐标
  • [1,3,6,7,12] 纵坐标

  • ‘ro’: r表示颜色为红色,o表示点用o表示

绘图自定义样式:
自定义样式

1.3 绘制多组数据

plt.figure(figsize=[4, 4])
plt.plot([1,3,5,7,9], [1,3,6,7,12], 'ro')
plt.plot([2,4,6,8,10], [4,7,12,15,20], 'g-')

outputs:

[<matplotlib.lines.Line2D at 0x25797540a30>]

png

1.4 绘制图例信息

查看当前plt的参数

plt.rcParams.keys()
KeysView(RcParams({'_internal.classic_mode': False,
		  ...
          'font.cursive': ['Apple Chancery',
                           'Textile',
                           'Zapf Chancery',
                           'Sand',
                           'Script MT',
                           'Felipa',
                           'Comic Neue',
                           'Comic Sans MS',
                           'cursive'],
          'font.family': ['Times New Roman'],
          'font.fantasy': ['Chicago',
                           'Charcoal',
                           'Impact',
                           'Western',
                           'Humor Sans',
                           'xkcd',
                           'fantasy'],
          'font.monospace': ['DejaVu Sans Mono',
                             'Bitstream Vera Sans Mono',
                             'Computer Modern Typewriter',
                             'Andale Mono',
                             'Nimbus Mono L',
                             'Courier New',
                             'Courier',
                             'Fixed',
                             'Terminal',
                             'monospace'],
          'font.sans-serif': ['KaiTi'],
          'font.serif': ['DejaVu Serif',
                         'Bitstream Vera Serif',
                         'Computer Modern Roman',
                         'New Century Schoolbook',
                         'Century Schoolbook L',
                         'Utopia',
                         'ITC Bookman',
                         'Bookman',
                         'Nimbus Roman No9 L',
                         'Times New Roman',
                         'Times',
                         'Palatino',
                         'Charter',
                         'serif'],
          'font.size': 10.0,
          ...
          'ytick.right': False}))
from matplotlib.font_manager import FontProperties

plt.figure(figsize=[3,3])
kaiti = FontProperties("KaiTi")  # 楷体字体
plt.rcParams['axes.unicode_minus'] = False # 显示负号
plt.plot([-1,-2,3,4,5], [1,2,3,6,10], 'g*',label="label1")  # 绘图
plt.plot([1,3,4,-6,7], [5,1,3,7,9], 'ro',label="label2")  # 绘图
plt.title('标题', fontproperties=kaiti) # 中文字体
plt.xlabel('X')
plt.ylabel('Y')
plt.legend(loc='best')
plt.show()

png

1.5 子图绘制

from numpy.random import randint
kaiti = FontProperties("KaiTi", size=20)  # 楷体字体

plt.figure(figsize=[16,4])
plt.suptitle("子图绘制", fontproperties=kaiti)

for t in range(4):
    plt.subplot(1, 4, t+1)
    plt.plot(randint(0, 10, 5), randint(0, 10, 5), 'g*',label="label1")  # 绘图
    plt.plot(randint(0, 10, 5), randint(0, 10, 5), 'ro',label="label2")  # 绘图
    plt.title('标题', fontproperties=kaiti) # 中文字体
    plt.xlabel('X')
    plt.ylabel('Y')
    plt.legend(loc='best')
plt.show()

png
子图绘制布局

plt.figure()
plt.subplot(2,4,1)
plt.gca().set(ylim=(-2.0, 1), xlim=(0, 1))
plt.subplot(1,2,2)
plt.gca().set(ylim=(-2.0, 1), xlim=(0, 1))
plt.subplot(4,4,2)
plt.gca().set(ylim=(-2.0, 1), xlim=(0, 1))
plt.subplot(2,2,3)
plt.gca().set(ylim=(-2.0, 1), xlim=(0, 1))
plt.subplot(4,8,11)
plt.gca().set(ylim=(-2.0, 1), xlim=(0, 1))
plt.subplot(4,8,12)
plt.gca().set(ylim=(-2.0, 1), xlim=(0, 1))
[(-2.0, 1.0), (0.0, 1.0)]

png

3. 使用axes绘图

plt绘图和axes绘图的区别:

  • plt绘图:先生成了一个Figure画布,然后在这个画布上隐式生成一个画图区域进行画图。
  • axes绘图:同时生成了Figure和axes两个对象,然后用axes对象在其区域内进行绘图。

figure相当于画布,axes相当于画布中的某个区域。

二者绘制的图像是一样的,但是对图像的零部件进行绘制时使用axes绘图更加方便。

3.1 axes绘图区域

fig = plt.figure()
ax1 = plt.subplot2grid((3,3), (0,0), colspan=2, rowspan=2)
ax1 = plt.subplot2grid((3,3), (0,2), rowspan=3)
ax2 = plt.subplot2grid((3,3), (2,0))
ax3 = plt.subplot2grid((3,3), (2,1))


png

3.2 axes绘图

kaiti = FontProperties("KaiTi", size=12)  # 楷体字体

x = np.arange(-2*np.pi, 2*np.pi, 0.01)
y1 = np.sin(x)
y2 = np.cos(x)

fig = plt.figure(figsize=(10, 5))
ax1, ax2 = fig.subplots(1, 2)
ax1.plot(x, y1, label='label1')
ax1.plot(x, y2, label='label2')
ax1.legend(loc='best')
ax1.set_xlim(-2 * np.pi - 1, 2 * np.pi + 3)
ax1.set_xticks([-2 * np.pi, -np.pi, 0, np.pi, 2 * np.pi])
ax1.set_xticklabels(['-pi/2', '-pi', '0', 'pi', 'pi/2'])
ax1.set_title('标题', fontproperties=kaiti)
ax1.set_xlabel('x轴', fontproperties=kaiti)
ax1.set_ylabel('y轴', fontproperties=kaiti)
ax1.axhline(y=0, c='black')
ax1.axvline(x=0, c='black')

ax2.matshow(np.random.randint(0, 255, (12, 12)), vmin=0, vmax=255, cmap='gray')

fig.suptitle("Axes绘图示例", fontproperties=kaiti)

plt.show()

png

4. 函数绘图

4.1 三角函数

plt.figure(figsize=[16, 4])

plt.subplot(1, 4, 1)
x = np.array([1,2,3,4,5])
y = np.sin(x)
plt.plot(x, y)

plt.subplot(1, 4, 2)
x = np.array([1,2,3,4,5])
y = np.cos(x)
plt.plot(x, y)

plt.subplot(1, 4, 3)
x = np.array([1,2,3,4,5])
y = np.arctan(x)
plt.plot(x, y)

plt.subplot(1, 4, 4)
t=np.arange(0, 2*np.pi, np.pi/50)
x=np.sin(t);
y=np.cos(t);
plt.plot(x, y)

outputs:

[<matplotlib.lines.Line2D at 0x2579a44dfa0>]

png

4.2 柱状图直方图饼图阶梯图

data = pd.read_csv("data/Eueo2012.csv")
data

outputs:

Team Goals Shots on target Shots off target Shooting Accuracy % Goals-to-shots Total shots (inc. Blocked) Hit Woodwork Penalty goals Penalties not scored ... Saves made Saves-to-shots ratio Fouls Won Fouls Conceded Offsides Yellow Cards Red Cards Subs on Subs off Players Used
0 Croatia 4 13 12 51.9% 16.0% 32 0 0 0 ... 13 81.3% 41 62 2 9 0 9 9 16
1 Czech Republic 4 13 18 41.9% 12.9% 39 0 0 0 ... 9 60.1% 53 73 8 7 0 11 11 19
2 Denmark 4 10 10 50.0% 20.0% 27 1 0 0 ... 10 66.7% 25 38 8 4 0 7 7 15
3 England 5 11 18 50.0% 17.2% 40 0 0 0 ... 22 88.1% 43 45 6 5 0 11 11 16
4 France 3 22 24 37.9% 6.5% 65 1 0 0 ... 6 54.6% 36 51 5 6 0 11 11 19
5 Germany 10 32 32 47.8% 15.6% 80 2 1 0 ... 10 62.6% 63 49 12 4 0 15 15 17
6 Greece 5 8 18 30.7% 19.2% 32 1 1 1 ... 13 65.1% 67 48 12 9 1 12 12 20
7 Italy 6 34 45 43.0% 7.5% 110 2 0 0 ... 20 74.1% 101 89 16 16 0 18 18 19
8 Netherlands 2 12 36 25.0% 4.1% 60 2 0 0 ... 12 70.6% 35 30 3 5 0 7 7 15
9 Poland 2 15 23 39.4% 5.2% 48 0 0 0 ... 6 66.7% 48 56 3 7 1 7 7 17
10 Portugal 6 22 42 34.3% 9.3% 82 6 0 0 ... 10 71.5% 73 90 10 12 0 14 14 16
11 Republic of Ireland 1 7 12 36.8% 5.2% 28 0 0 0 ... 17 65.4% 43 51 11 6 1 10 10 17
12 Russia 5 9 31 22.5% 12.5% 59 2 0 0 ... 10 77.0% 34 43 4 6 0 7 7 16
13 Spain 12 42 33 55.9% 16.0% 100 0 1 0 ... 15 93.8% 102 83 19 11 0 17 17 18
14 Sweden 5 17 19 47.2% 13.8% 39 3 0 0 ... 8 61.6% 35 51 7 7 0 9 9 18
15 Ukraine 2 7 26 21.2% 6.0% 38 0 0 0 ... 13 76.5% 48 31 4 5 0 9 9 18

16 rows × 35 columns

plt.figure(figsize=[16, 4])

x = data["Goals"]
y = data["Shots on target"]
plt.subplot(1, 4, 1)
plt.bar(x,y, width = 0.3,color = 'red')  # 柱状图

x = data["Goals"]
plt.subplot(1, 4, 2)
plt.hist(x,bins=10, rwidth=0.8,histtype='barstacked',color='r')  # 柱状图

x = data["% Goals-to-shots"].str.strip("%").astype(float) / 100  # %转数字
label = data["Team"]  #定义饼图的标签
plt.subplot(1, 4, 3)
plt.pie(x, labels=label,autopct='%.3f%%')

x = [1,2,3,4,5]
y = [3,5,7,9,13]
plt.subplot(1, 4, 4)
plt.step(x, y)

outputs:

[<matplotlib.lines.Line2D at 0x257a50507c0>]

png

5.特殊图形

5.1 气泡图

midwest = pd.read_csv("data/midwest_filter.csv")
kaiti = FontProperties("KaiTi", size=24)  # 楷体字体

fig = plt.figure(figsize=(14, 7), dpi= 80, facecolor='w', edgecolor='k')
plt.scatter('area', 'poptotal', data=midwest, 
            s='dot_size', c='popdensity', 
            cmap='Reds', edgecolors='black', linewidths=.5)
plt.title("气泡图 PopTotal vs Area", fontproperties=kaiti)
plt.xlabel('面积', fontsize=18, fontproperties=kaiti)
plt.ylabel('人口总数', fontsize=18, fontproperties=kaiti)
plt.colorbar()
plt.show()

png

5.2 气泡彩图

midwest = pd.read_csv("data/midwest_filter.csv")
kaiti = FontProperties("KaiTi", size=24)  # 楷体字体

fig = plt.figure(figsize=(14, 9), dpi= 80, facecolor='w', edgecolor='k')
colors = plt.cm.tab20.colors
categories = np.unique(midwest['category'])
for i, category in enumerate(categories):
    plt.scatter('area', 'poptotal', data=midwest.loc[midwest.category==category, :], 
            s='dot_size', label=str(category), edgecolors='black', linewidths=.5)
    
for dot_size in [100, 300, 1000]:
    plt.scatter([], [], c='k', alpha=0.5, s=dot_size, label=str(dot_size) + ' TotalPop')
plt.legend(loc='upper right', scatterpoints=1, 
           frameon=False, labelspacing=2, 
           title='Saravana Stores', fontsize=8)
plt.title("气泡彩图 PopTotal vs Area", fontproperties=kaiti)
plt.xlabel('面积', fontsize=18, fontproperties=kaiti)
plt.ylabel('人口总数', fontsize=18, fontproperties=kaiti)
plt.show()

png

5.3 时间序列

plt.figure()
ax = plt.subplot2grid((2,4), (0,0), colspan=8)
x = np.linspace(-0.75, 1., 100)
dates = pd.date_range('2018-01-01', periods = len(x))
ax.plot(dates, x + np.random.randn(len(x)))
ax.set_xticks(dates[::30])
ax.set_xticklabels(dates.strftime('%Y-%m-%d')[::30])
ax.set_title("Time Series")

outputs:

Text(0.5, 1.0, 'Time Series')

png


文章作者: 易安
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 易安 !
评论
  目录