UP | HOME

Matplotlib 第一课

Table of Contents

1 Matplotlib

1.1 Plotting

import numpy as np
import matplotlib.pyplot as plt
x= np.arange(0, 3*np.pi, 0.1)
print(type(x))

y= np.sin(x)
print(type(y))

plt.plot(x, y)
plt.show()
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>

x= np.arange(0, 3*np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)

plt.plot(x, y_sin)
plt.plot(x, y_cos)
plt.xlabel('x axis label')
plt.xlabel('y axis label')
plt.title('Sine and Cosine')
plt.legend(['sine', 'cosine'])
plt.show()

1.2 Subplots

import numpy as np
import matplotlib.pyplot as plt

x= np.arange(0, 3*np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)

plt.subplot(2, 2, 1)
plt.plot(x,y_sin)
plt.title('sine')

plt.subplot(2, 2, 2)
plt.plot(x,y_cos)
plt.title('cosine')

plt.show()
plt.subplot(shape,location)
subplot(2,1,1): 2 rows 1 column, my next plot image is at 1st location
.    this is *subplot(2, 1, x)* image
.
.    +-------------------------------+
.    |     ....             ..       |
.    |   ...  ...         ......     | <------- subplot(2, 1, 1)
.    |   .      ...     ...    ...   |
.    |  ..         ......        ..  |
.    +----...------..----------------+
.    | .... ...         .....        |
.    | .      ..      ...    ...     | <------- subplot(2, 1, 2)
.    |         ...  ...        ..    |
.    |           ....            ..  |
.    +-------------------------------+


.    this is *subplot(2, 2, x)* image
.
.              +------------------------------- subplot(2, 2, 1)
.              |
.              v
.    +------ -------.-+---------------+
.    |              ..|..          .. |
.    |  ..         .. | ..        ..  | <------ subplot(2, 2, 2)
.    |.... .     ...  |  ..     ...   |
.    |.    . .....    |   .......     |
.    +-....- ---------+---------------+
.    |.. ..           | ..            |
.    |   ..           |  ...          |
.    |    ..          |    ...      . | <------ subplot(2, 2, 4)
.    |     . ....     |      ...  ... |
.    +------ ---------+---------------+
.              ^
.              |
.              +------------------------------- subplot(2, 2, 3)

1.3 Images

use imshow function to show images.

import numpy as np
from scipy.misc import imread, imresize
import matplotlib.pyplot as plt

img = imread('data/chenhongqi3.jpg')
img_tinted = img * [1, 0.5, 0.5]

plt.subplot(1, 2, 1)
plt.imshow(img)

plt.subplot(1, 2, 2)

plt.imshow(np.uint8(img_tinted))
plt.show()

1.4 Task

import numpy as np
from scipy.misc import imread, imsave, imresize
import matplotlib.pyplot as plt
img = imread('data/chenhongqi3.jpg')

print (img.shape)
# reashape the img to 1 dimension vector
length, height, depth = img.shape
img_reshaped = np.reshape(img, (length * height * 3, 1))

x_axis = np.arange(0, 255, 1)
def cnt(x):
    cnt_arr = np.zeros(x.shape, dtype=int)
    for i in x:
        cnt_arr[i] = img_reshaped[(img_reshaped == x[i])].size
    return cnt_arr

y_axis = cnt(x_axis)

plt.plot(y_axis)
plt.show()

Note that: If give ONLY ONE set as parameter of plot, this set will be seen default as 'y' , the index will default be the 'x'

(1920, 1280, 3)

x = np.arange(0, 255, 1)
print(x)
y = x[(x == 3)].size
print (y)
[  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35
  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53
  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71
  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89
  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107
 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
 252 253 254]
1