day26-3 matplatlib模块

matplotlib

  • 图形可视化,主要用来画图
  • 别问,问就是看不懂

条形图

import matplotlib.pyplot as plt
# 只识别英语,所以通过以下两行增加中文字体
from matplotlib.font_manager import FontProperties
# 字体路径根据电脑而定
font = FontProperties(fname='M:\STKAITI.TTF')

# jupyter 默认不显示图片,通过这一行告诉他显示
%matplotlib inline


classes = ['1班', '2班', '3班', '4班'] # 相当于columns
student_amounts = [30, 20, 30, 40]   # 值
classes_index = range(len(classes))    # [0, 1, 2, 3]

plt.bar(classes_index, student_amounts)
plt.xticks(classes_index, classes, FontProperties=font)

for ind,student_amount in enumerate(student_amounts):
    print(ind,student_amount)
    plt.text(ind,student_amount+1,student_amount)


plt.xlabel('班级', FontProperties=font)
plt.ylabel('学生人数', FontProperties=font)
plt.title('班级-学生人数', FontProperties=font)

plt.show()
0 30
1 20
2 30
3 40

png

## 直方图

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
%matplotlib inline
font = FontProperties(fname='M:\STKAITI.TTF')

mu1, mu2, sigma = 50, 100, 10
x1 = mu2 + sigma * np.random.randn(10000)
print(x1)
[ 93.49947877  86.87378653  98.0194217  ... 108.33555519  90.58512015
 102.19048574]
x1 = np.random.randn(10000)
print(x1)
[ 0.85927045 -0.8061112   1.30878058 ... -0.32700199 -0.67669564
  0.25750884]
x2 = mu2 + sigma*np.random.randn(10000)
print(x2)
[101.62589858 109.86489987 117.41374105 ...  97.52364544 107.21076273
  99.56765772]
plt.hist(x1, bins=100)

plt.hist(x2, bins=100)
plt.show()

png

plt.style.use('ggplot')

fig = plt.figure()

# 相当于把一整块画板分成了1行2列的两个画板
ax1 = fig.add_subplot(121)
ax1.hist(x1, bins=100, color='red')
ax1.set_title('红色', fontproperties=font)

ax2 = fig.add_subplot(122)
ax2.hist(x2, bins=100, color='yellow')
ax2.set_title('黄色', fontproperties=font)

fig.suptitle('大标题', fontproperties=font, fontsize=15, weight='bold')
plt.show()

png

折线图

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
%matplotlib inline
font = FontProperties(fname='M:\STKAITI.TTF')
plt.style.use('ggplot')

np.random.seed(1)
data1 = np.random.rand(40).cumsum()
data2 = np.random.rand(40).cumsum()
data3 = np.random.rand(40).cumsum()
data4 = np.random.rand(40).cumsum()
plt.plot(data1, color='r', linestyle='-', alpha=0.5, label='红色')
plt.plot(data2, color='green', linestyle='--', label='绿色')
plt.plot(data3, color='yellow', linestyle=':', label='黄色')
plt.plot(data4, color='blue', linestyle='-.', label='蓝色')

plt.legend(prop=font)

plt.show()

png

arr = np.array([1, 2, 3, 4])
arr.cumsum()#   1,1+2,1+2+3,1+2+3+4
array([ 1,  3,  6, 10], dtype=int32)

散点图

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
%matplotlib inline
font = FontProperties(fname='M:\STKAITI.TTF')
x = np.arange(1, 20)
x
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19])
y_linear = x**2
y_linear
array([  1,   4,   9,  16,  25,  36,  49,  64,  81, 100, 121, 144, 169,
       196, 225, 256, 289, 324, 361], dtype=int32)
y_log = np.log(x)
y_log
array([0.        , 0.69314718, 1.09861229, 1.38629436, 1.60943791,
       1.79175947, 1.94591015, 2.07944154, 2.19722458, 2.30258509,
       2.39789527, 2.48490665, 2.56494936, 2.63905733, 2.7080502 ,
       2.77258872, 2.83321334, 2.89037176, 2.94443898])
fig = plt.figure()

ax1 = fig.add_subplot(311)
ax1.scatter(x, y_linear, color='red', marker='o', s=100)
ax1.scatter(x, y_log, color='blue', marker='*', s=30)
ax1.set_title('scatter')

ax2 = fig.add_subplot(313)
ax2.plot(x, y_linear)
ax2.plot(x, y_log)
ax2.set_title('plot')

plt.plot
plt.show()

png

猜你喜欢

转载自www.cnblogs.com/lucky75/p/11011617.html