matplotlib模块
一、导入方式
import matplotlib.pyplot as plt # 默认支持英文,不支持中文
二、作用
可视化分析
三、方法
中文转英文的方法
from matplotlib.font_manager import FontProperties font = FontProperties(fname='中文字体所在的路径')
3.1 条形图
import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties %matplotlib inline font = FontProperties(fname='/Library/Fonts/Heiti.ttc') # 修改背景为条纹 plt.style.use('ggplot') classes = ['3班', '4班', '5班', '6班'] classes_index = range(len(classes)) print(list(classes_index)) student_amounts = [66, 55, 45, 70] # 画布设置 fig = plt.figure() # 1,1,1表示一张画布切割成1行1列共一张图的第1个;2,2,1表示一张画布切割成2行2列共4张图的第一个(左上角) ax1 = fig.add_subplot(1, 1, 1) ax1.bar(classes_index, student_amounts, align='center', color='darkblue') ax1.xaxis.set_ticks_position('bottom') ax1.yaxis.set_ticks_position('left') plt.xticks(classes_index, classes, rotation=0, fontsize=13, fontproperties=font) plt.xlabel('班级', fontproperties=font, fontsize=15) plt.ylabel('学生人数', fontproperties=font, fontsize=15) plt.title('班级-学生人数', fontproperties=font, fontsize=20) # 保存图片,bbox_inches='tight'去掉图形四周的空白 # plt.savefig('classes_students.png?x-oss-process=style/watermark', dpi=400, bbox_inches='tight') plt.show()
3.2 直方图
import numpy as np import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties %matplotlib inline font = FontProperties(fname='/Library/Fonts/Heiti.ttc') # 修改背景为条纹 plt.style.use('ggplot') mu1, mu2, sigma = 50, 100, 10 # 构造均值为50的符合正态分布的数据 x1 = mu1 + sigma * np.random.randn(10000) print(x1) # 构造均值为100的符合正态分布的数据 x2 = mu2 + sigma * np.random.randn(10000) print(x2) fig = plt.figure() ax1 = fig.add_subplot(121) # bins=50表示每个变量的值分成50份,即会有50根柱子 ax1.hist(x1, bins=50, color='darkgreen') ax2 = fig.add_subplot(122) ax2.hist(x2, bins=50, color='orange') fig.suptitle('两个正态分布', fontproperties=font, fontweight='bold', fontsize=15) ax1.set_title('绿色的正态分布', fontproperties=font) ax2.set_title('橙色的正态分布', fontproperties=font) plt.show()
3.3 折线图
import numpy as np from numpy.random import randn import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties %matplotlib inline font = FontProperties(fname='/Library/Fonts/Heiti.ttc') # 修改背景为条纹 plt.style.use('ggplot') np.random.seed(1) # 使用numpy的累加和,保证数据取值范围不会在(0,1)内波动 plot_data1 = randn(40).cumsum() print(plot_data1) plot_data2 = randn(40).cumsum() plot_data3 = randn(40).cumsum() plot_data4 = randn(40).cumsum() plt.plot(plot_data1, marker='o', color='red', linestyle='-', label='红实线') plt.plot(plot_data2, marker='x', color='orange', linestyle='--', label='橙虚线') plt.plot(plot_data3, marker='*', color='yellow', linestyle='-.', label='黄点线') plt.plot(plot_data4, marker='s', color='green', linestyle=':', label='绿点图') # loc='best'给label自动选择最好的位置 plt.legend(loc='best', prop=font) plt.show()
3.4 散点图+直线图
import numpy as np from numpy.random import randn import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties %matplotlib inline font = FontProperties(fname='/Library/Fonts/Heiti.ttc') # 修改背景为条纹 plt.style.use('ggplot') x = np.arange(1, 20, 1) print(x) # 拟合一条水平散点线 np.random.seed(1) y_linear = x + 10 * np.random.randn(19) print(y_linear) # 拟合一条x²的散点线 y_quad = x**2 + 10 * np.random.randn(19) print(y_quad) # s是散点大小 fig = plt.figure() ax1 = fig.add_subplot(121) plt.scatter(x, y_linear, s=30, color='r', label='蓝点') plt.scatter(x, y_quad, s=100, color='b', label='红点') ax2 = fig.add_subplot(122) plt.plot(x, y_linear, color='r') plt.plot(x, y_quad, color='b') # 限制x轴和y轴的范围取值 plt.xlim(min(x) - 1, max(x) + 1) plt.ylim(min(y_quad) - 10, max(y_quad) + 10) fig.suptitle('散点图+直线图', fontproperties=font, fontsize=20) ax1.set_title('散点图', fontproperties=font) ax1.legend(prop=font) ax2.set_title('直线图', fontproperties=font) plt.show()
3.5 饼图
import numpy as np import matplotlib.pyplot as plt from pylab import mpl mpl.rcParams['font.sans-serif'] = ['SimHei'] fig, ax = plt.subplots(subplot_kw=dict(aspect="equal")) recipe = ['优', '良', '轻度污染', '中度污染', '重度污染', '严重污染', '缺'] data = [2, 49, 21, 9, 11, 6, 2] colors = ['lime', 'yellow', 'darkorange', 'red', 'purple', 'maroon', 'grey'] wedges, texts, texts2 = ax.pie(data, wedgeprops=dict(width=0.5), startangle=40, colors=colors, autopct='%1.0f%%', pctdistance=0.8) plt.setp(texts2, size=14, weight="bold") bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=0.72) kw = dict(xycoords='data', textcoords='data', arrowprops=dict(arrowstyle="->"), bbox=None, zorder=0, va="center") for i, p in enumerate(wedges): ang = (p.theta2 - p.theta1) / 2. + p.theta1 y = np.sin(np.deg2rad(ang)) x = np.cos(np.deg2rad(ang)) horizontalalignment = {-1: "right", 1: "left"}[int(np.sign(x))] connectionstyle = "angle,angleA=0,angleB={}".format(ang) kw["arrowprops"].update({"connectionstyle": connectionstyle}) ax.annotate(recipe[i], xy=(x, y), xytext=(1.25 * np.sign(x), 1.3 * y), size=16, horizontalalignment=horizontalalignment, fontproperties=font, **kw) ax.set_title("饼图示例",fontproperties=font) plt.show() # plt.savefig('jiaopie2.png?x-oss-process=style/watermark')
3.6 箱型图
import numpy as np import pandas as pd from numpy.random import randn import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties %matplotlib inline font = FontProperties(fname='/Library/Fonts/Heiti.ttc') df = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E']) plt.figure(figsize=(10, 4)) # 创建图表、数据 f = df.boxplot( sym='o', # 异常点形状,参考marker vert=True, # 是否垂直 whis=1.5, # IQR,默认1.5,也可以设置区间比如[5,95],代表强制上下边缘为数据95%和5%位置 patch_artist=True, # 上下四分位框内是否填充,True为填充 meanline=False, showmeans=True, # 是否有均值线及其形状 showbox=True, # 是否显示箱线 showcaps=True, # 是否显示边缘线 showfliers=True, # 是否显示异常值 notch=False, # 中间箱体是否缺口 return_type='dict' # 返回类型为字典 ) plt.title('boxplot') for box in f['boxes']: box.set(color='b', linewidth=1) # 箱体边框颜色 box.set(facecolor='b', alpha=0.5) # 箱体内部填充颜色 for whisker in f['whiskers']: whisker.set(color='k', linewidth=0.5, linestyle='-') for cap in f['caps']: cap.set(color='gray', linewidth=2) for median in f['medians']: median.set(color='DarkBlue', linewidth=2) for flier in f['fliers']: flier.set(marker='o', color='y', alpha=0.5) # boxes, 箱线 # medians, 中位值的横线, # whiskers, 从box到error bar之间的竖线. # fliers, 异常值 # caps, error bar横线 # means, 均值的横线
四、图像标注参数
设置图像标题 | plt.title() |
---|---|
设置x轴名称 | plt.xlabel() |
设置y轴名称 | plt.ylabel() |
设置X轴范围 | plt.xlim() |
设置Y轴范围 | plt.ylim() |
设置X轴刻度 | plt.xticks() |
设置Y轴刻度 | plt.yticks() |
设置曲线图例 | plt.legend() |
来源:https://www.cnblogs.com/yanjiayi098-001/p/11378214.html