day26-3 matplatlib模块

时间:2019-06-12
本文章向大家介绍day26-3 matplatlib模块,主要包括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

## 直方图

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

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

折线图

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

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

$flag 上一页 下一页

上一篇:qt paintEvent

下一篇:jvm03