Python之Matplotlib視覺化

一。正弦圖&餘弦圖

# -*- coding: utf-8 -*-

“”“

Created on Thu Jul 02 21:37:47 2020

@author: zhen

”“”

“”“

正弦和餘弦圖

”“”

import matplotlib。pyplot as plt

import numpy as np

x = np。linspace(-np。pi, np。pi, 256, endpoint=True)

y_cos = np。cos(x)

y_sin = np。sin(x)

plt。plot(x, y_cos)

plt。plot(x, y_sin)

title(“Function $\sin$ and $\cos$”)

xlim(-3。6, 3。6)

ylim(-1。0, 1。0)

# format ticks at specific values

xticks([-np。pi, -np。pi/2, 0, np。pi/2, np。pi],[r‘$-\pi$’, r‘$-\pi/2$’, r‘$0$’, r‘$+pi/2$’, r‘$+\pi$’])

yticks([-1, 0, +1],[r‘$-1$’, r‘$0$’, r‘$+1$’])

plt。show()

效果:

Python之Matplotlib視覺化

二。箱線圖&柱狀圖

from pylab import *

dataset = [113, 115, 119, 121, 124,

124, 125, 126, 126, 126,

127, 127, 128, 129, 130,

130, 131, 132, 133, 136]

subplot(121)

boxplot(dataset, vert=False)

subplot(122)

hist(dataset)

show()

效果:

Python之Matplotlib視覺化

三。組合使用

x = [1, 2, 3, 4]

y = [5, 4, 3, 2]

# create new figure

figure()

# divide subplots into 2 * 3 grid

# and select #1

subplot(231)

plot(x, y)

# select #2

subplot(232)

bar(x, y)

# horizontal bar-charts

subplot(233)

barh(x, y)

# create stacked bar charts

subplot(234)

bar(x, y)

# we need more data for stacked bar charts

y1 = [7, 8, 5, 3]

bar(x, y1, bottom=y, color=‘r’)

# box plot

subplot(235)

boxplot(x)

# scatter plot

subplot(236)

scatter(x, y)

show()

效果:

Python之Matplotlib視覺化