1、
from numpy import *
from pylab import *
x = linspace(-3, 3, 30)
y = x ** 2
plt.title('test')
'''
如要绘制符号而不是线,可用的符号参数如下:
符号:^ , v , < , > , s , + , x , D , d , 1 , 2 , 3 , 4 ,
h , H , p , | , _ , - , –, -., , . , , , o ,
颜色:b, g, r, c, m, y, k, w
'''
from pylab import *
x = linspace(-3, 3, 30)
# y = x**2
# plot(x, y,'y|')
# 绘制多个图形
plot(x, sin(x))
plot(x, cos(x), 'r-')
plot(x, -sin(x), 'g--')
show()
2、
import matplotlib.pyplot as plt
import numpy as np
# 显示中文设置...
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 一行三个图形,画布大小长9宽4,单个长3宽4
fig, axes = plt.subplots(1, 3, figsize=(9, 4))
x = np.arange(1, 11)
axes[0].plot(x, x ** 3, 'g', lw=1) # lw设置线条粗细
axes[0].grid(True) # 设置网格
axes[0].set_title('默认网格')
axes[1].plot(x, np.exp(x), 'r')
axes[1].grid(color='b', ls='-.', lw=0.25) # ls设置线条的样式
axes[1].set_title('自定义网格')
axes[2].plot(x, x)
axes[2].set_title('无网格')
# tight_layout会自动调整子图参数,使之填充整个图像区域
fig.tight_layout()
plt.show()
# 设置行列的取值大小
# ax.set_xlim(0,10)
# ax.set_ylim(0,10000)
3、
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
girls_grades = [89, 90, 70, 89, 100, 80, 90, 100, 80, 34]
boys_grades = [30, 29, 49, 48, 100, 48, 38, 45, 20, 30]
grades_range = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
# plt.legend(labels = ('男孩','女孩'),loc='upper left')
colors=np.random.rand(10)
plt.scatter(grades_range, girls_grades, color='r', alpha=0.5) # alpha取值在[0-1]之间,表示该颜色的深浅程度
plt.scatter(grades_range, boys_grades, colors)
plt.title('散点图示例') # 显示图表标题
plt.xlabel('分数范围') # x轴名称
plt.ylabel('分数等级') # y轴名称
plt.grid(True) # 显示网格线
plt.legend(labels=('男孩', '女孩'), loc='upper right')
plt.show()
4、
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei'] # 显示中文
plt.rcParams['axes.unicode_minus'] = False # 解决坐标轴负数的负号显示问题
x = np.arange(0, np.pi * 2, 0.05)
y = np.sin(x)
plt.plot(x, y) # 绘制两个数组的值
plt.xlabel('角度') # X轴标签
plt.ylabel('正弦') # Y轴标签
plt.title('正弦波') # 绘图标题
plt.show() # 调用绘图查看器窗口
# fig, a = plt.subplots(2, 2) # 二行二列个子图