【python】matplotlib库的使用
参考自:嵩天老师的《python数据分析与展示》
1. 折线图
"""
plt.plot(x, y, format_string, **kwargs)
x : X轴数据,列表或数组,可选
y : Y轴数据,列表或数组
format_string : 控制曲线的格式字符串,可选,详情请参照最后的说明
**kwargs : 第二组或更多(x,y,format_string)
当绘制多条曲线时,各条曲线的x不能省略
"""
import matplotlib.pyplot as plt
plt.plot([0, 2, 4, 6, 8], [3, 1, 4, 5, 2])
plt.xlabel('Time')
plt.ylabel('Grade')
plt.axis([-1, 10, 0, 6])
plt.savefig('1', dpi = 300)
plt.show()
import matplotlib.pyplot as plt
import numpy as np
a = np.arange(10)
plt.plot(a, a*1.5, a, a*2.5, a, a*3.5, a, a*4.5)
plt.savefig('3', dpi = 100)
plt.show()
2. 散点图
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
ax.plot(10*np.random.randn(100), 10*np.random.randn(100), 'o')
ax.set_title('Simple Scatter')
plt.savefig('1', dpi = 300)
plt.show()
import matplotlib.pyplot as plt
import numpy as np
RGBA = [10, 10, 10, 200]
plt.title("气泡图", fontproperties='SimHei', fontsize=12, color=[num / 255 for num in RGBA])
x = 10 * np.random.randn(100)
y = 10 * np.random.randn(100)
size = 10 * np.random.randint(10, 90, 100)
plt.scatter(x, y, s=size, c="red", alpha=0.6)
plt.show()
plt.close()
3. 直方图
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(0)
mu, sigma = 100, 20
a = np.random.normal(mu, sigma, size=100)
plt.hist(a, 20, density=1, histtype='stepfilled', facecolor='b', alpha=0.75)
plt.title('Histogram')
plt.savefig('1', dpi = 300)
plt.show()
4. 饼图
import matplotlib.pyplot as plt
import numpy as np
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0)
plt.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=False, startangle=90)
plt.axis('tight')
plt.savefig('1', dpi = 300)
plt.show()
import matplotlib.pyplot as plt
import numpy as np
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0)
plt.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=False, startangle=90)
plt.savefig('2', dpi = 300)
plt.show()
5. 子绘图区域
import matplotlib.pyplot as plt
import numpy as np
a = np.arange(0.0, 5.0, 0.02)
def cos():
plt.plot(a, np.cos(2*np.pi*a))
plt.subplot2grid((3,3), (0,0), colspan=3)
cos()
plt.subplot2grid((3,3), (1,0), colspan=2)
cos()
plt.subplot2grid((3,3), (1,2), rowspan=3)
cos()
plt.subplot2grid((3,3), (2,0))
cos()
plt.subplot2grid((3,3), (2,1))
cos()
plt.savefig('1', dpi = 300)
plt.show()
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np
a = np.arange(0.0, 5.0, 0.02)
def cos():
plt.plot(a, np.cos(2*np.pi*a), 'r')
gs = gridspec.GridSpec(3,3)
ax1 = plt.subplot(gs[0, :])
cos()
ax1 = plt.subplot(gs[1, :-1])
cos()
ax1 = plt.subplot(gs[1:, -1])
cos()
ax1 = plt.subplot(gs[2, 0])
cos()
ax1 = plt.subplot(gs[2, 1])
cos()
plt.savefig('2', dpi = 300)
plt.show()
6. 中文显示
import matplotlib.pyplot as plt
import matplotlib
'''
'SimHei' 黑体
'Kaiti' 楷体
'LiSu' 隶书
'FangSong' 仿宋
'YouYuan' 幼圆
'STSong' 华文宋体
'''
matplotlib.rcParams['font.family'] = 'SimHei'
matplotlib.rcParams['font.style'] = 'italic'
matplotlib.rcParams['font.size'] = 20
plt.plot([3,1,4,5,2])
plt.xlabel('时间')
plt.ylabel('成绩')
plt.axis([-1,5,0,6])
plt.savefig('1', dpi = 300)
plt.show()
import matplotlib.pyplot as plt
import numpy as np
a = np.arange(0.0, 5.0, 0.02)
plt.xlabel('横轴:时间', fontproperties='SimHei', fontsize=20)
plt.ylabel('纵轴:振幅', fontproperties='SimHei', fontsize=20)
plt.plot(a, np.cos(2*np.pi*a), 'r--')
plt.savefig('2', dpi = 300)
plt.show()
7. 文本显示
import matplotlib.pyplot as plt
import numpy as np
a = np.arange(0.0, 5.0, 0.02)
plt.plot(a, np.cos(2*np.pi*a), 'r--')
plt.xlabel('横轴:时间', fontproperties='SimHei', fontsize=15, color='green')
plt.ylabel('纵轴:振幅', fontproperties='SimHei', fontsize=15)
plt.title(r'正弦波实例 $y=cos(2\pi x)$', fontproperties='SimHei', fontsize=25)
plt.text(2,1,r'$\mu=100$', fontsize=15)
plt.axis([-1,6,-2,2])
plt.grid(True)
plt.savefig('1', dpi = 300)
plt.show()
import matplotlib.pyplot as plt
import numpy as np
a = np.arange(0.0, 5.0, 0.02)
plt.plot(a, np.cos(2*np.pi*a), 'r--')
plt.xlabel('横轴:时间', fontproperties='SimHei', fontsize=15, color='green')
plt.ylabel('纵轴:振幅', fontproperties='SimHei', fontsize=15)
plt.title(r'正弦波实例 $y=cos(2\pi x)$', fontproperties='SimHei', fontsize=25)
plt.annotate(r'$\mu=100$', xy=(2, 1), xytext=(3,1.5),
arrowprops=dict(facecolor='black', shrink=0.1, width=2))
plt.axis([-1,6,-2,2])
plt.grid(True)
plt.savefig('2', dpi = 300)
plt.show()
说明
format_string : 控制曲线的格式字符串,可选;由颜色字符、 风格字符和标记字符组成 。