01 matplotlib - 折线图、绘图属性、Web安全色、子图、保存画板

简介: === 折线图 - 头文件 ===解决:1、中文显示问题2、图像行内外显示3、处理闪退问题import matplotlib.pyplot as pltimport numpy as npimport pandas as pd# 解决中文显示问题import matplotlib as mplmpl.

=== 折线图 - 头文件 ===

解决:
1、中文显示问题
2、图像行内外显示
3、处理闪退问题
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# 解决中文显示问题
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
# 使用自己的字体 - 定义字体
songTi = mpl.font_manager.FontProperties(fname='C:\Windows\Fonts\STHUPO.TTF')

# inline 在行内显示
# plt.show() 在行内显示
%matplotlib inline 

# tk 在图形化界面显示,但是有可能会出现闪退的问题
# 解决方法 plt.ion() plt.pause(10) plt.close()
#%matplotlib tk

=== 正常显示和闪退处理 ===

# plot([x轴坐标,y轴左边])
plt.plot([1,2,3,4],[2,1,1,3])

# 正常显示
plt.show()

# 解决闪退
#plt.ion()
# 10秒后自动关闭
#plt.pause(10)
#plt.close()

=== figure 图形(画板) ===

plt.figure()
plt.plot([1,2,3],[2,2,2])

# 新创建画板后,绘图会在新画板中显示
#plt.figure() 
plt.plot([1,2,3],[3,2,1])

=== 绘图属性 1 ===

# 绘制男女身高和体重的关系图 np.array
plt.figure()
boy_tall = np.array([150,160,170,180])
boy_weight = np.array([60,70,73,75])

girl_tall = np.array([150,160,170,180])
girl_weight = np.array([50,55,60,70])
1、color

========== ========
character color
========== ========
'b' blue
'g' green
'r' red
'c' cyan
'm' magenta
'y' yellow
'k' black
'w' white
========== ========

2、linewidth宽度
3、linestyle线类型 marker标记点

================ ===============================
character description
================ ===============================
'-' solid line style
'--' dashed line style
'-.' dash-dot line style
':' dotted line style
'.' point marker
',' pixel marker
'o' circle marker
'v' triangle_down marker
'^' triangle_up marker
'<' triangle_left marker
'>' triangle_right marker
'1' tri_down marker
'2' tri_up marker
'3' tri_left marker
'4' tri_right marker
's' square marker
'p' pentagon marker
'*' star marker
'h' hexagon1 marker
'H' hexagon2 marker
'+' plus marker
'x' x marker
'D' diamond marker
'd' thin_diamond marker
'|' vline marker
'_' hline marker
================ ===============================

4、makersize 标记点大小
5、label 图例,用lplt.legend()显示
# 绘图参数 color linewidth宽度 linestyle线类型 
# marker标记点 makersize 标记点大小
# label 图例 - 这一条线段表示什么 要用lplt.legend()显示
plt.plot(boy_tall,boy_weight,color='red',linewidth=5,label='boy')
plt.plot(girl_tall,girl_weight,color='green',linestyle='--',marker='o')

# 显示图例 即label
plt.legend()
plt.show()

=== 绘图属性 2 ===

1、简写:颜色b 线条 -. 标记o
2、设置标题 xlabel、ylabel
3、设置x轴、y轴范围 xlim、ylim
4、图例修改位置
plt.plot(girl_tall,girl_weight,'b-.o',label='女孩')
# 设置标题 
plt.xlabel('身高(cm)')
plt.ylabel('体重(kg)')

# 设置x轴、y轴范围
plt.xlim(145,185)
plt.ylim(20,100)

# 图例修改位置 upper lower left right 
plt.legend(loc ='upper left') #左上角
plt.show()

=== Web安全色 ===

如果使用编码设定颜色,颜色可以从该网址上查阅

http://www.bootcss.com/p/websafecolors/

=== 绘图属性 3 ===

1、刻度控制
2、大标题
3、改变字体
4、背景色
# 一年内每个季度的平均温度
# plt.figure(facecolor='red') #背景色
tem = np.array([20.1,34,26,10]) # 温度
quarter = np.arange(1,5) # 季度
plt.xlabel('季度')
plt.ylabel('温度')

# 直接画图会出现季度坐标会出现小数刻度
# xticks 控制刻度
# plt.xticks(quarter) # 小数点消失
plt.xticks(quarter,['第一季度','第二季度','第三季度','第四季度'])

# 大标题 - 改变字体 fontproperties
plt.title('季度的温度表',fontproperties=songTi)
plt.plot(quarter,tem)

=== 坐标轴与刻度可见度 ===

# 一年内每个季度的平均温度
tem = np.array([20.1,34,26,10]) # 温度
quarter = np.arange(1,5) # 季度
plt.xlabel('季度')
plt.ylabel('温度')
plt.xticks(quarter,['第一季度','第二季度','第三季度','第四季度'])
ax = plt.gca() #获取坐标轴
去除坐标轴 方法一
ax.spines['right'].set_color("none")
ax.spines['top'].set_color("none")
刻度只留左边 方法二
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
x轴 刻度只留底部
y轴 刻度只留左边
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.plot(quarter,tem)

=== subplot 子图 ===

在一个画板上画多个子图
1、直接设置子图
plt.figure()
# 成-4,5步长为0.1的数组
x = np.arange(-4,5,0.1)

# subplot(1,2,1):画在1行,2列,第1个位置
plt.subplot(1,2,1)
plt.plot(x,np.cos(x),label='cos')
plt.legend()

# subplot(1,2,2): 画在1行,2列,第2个位置
plt.subplot(1,2,2)
plt.plot(x,np.sin(x),label='sin')
plt.legend()

plt.show()
2、面向对象写法来创建子图
fig=plt.figure()
# 成-4,5步长为0.1的数组
x=np.arange(-4,5,0.1)

分别返回两个画布进行绘制ax1 ax2

fig=plt.figure()
ax1=fig.add_subplot(221)
ax2=fig.add_subplot(224)
ax1.plot(x,np.sin(x))
ax2.plot(x,np.cos(x))
3、先设置好图,再绘制
返回:一维数组
fig,ax = plt.subplots(4,1)
ax[0].plot(x,np.cos(x))
ax[2].plot(x,np.cos(x))
返回:二维数组
fig,ax = plt.subplots(2,2)
ax[0][0].plot(x,np.cos(x))
ax[1][1].plot(x,np.cos(x))
思考: 使用subplot返回的ax维度为何会有变化?
(4行1列) 一维 目字型
(2行2列) 二维 田字形

=== 保存画板 ===

plt.savefig('a.png')
相关文章
|
Web App开发 监控 安全
OSS客户端签名直传实践:Web端安全上传TB级文件方案(含STS临时授权)
本文深入解析了客户端直传技术,涵盖架构设计、安全机制、性能优化等方面。通过STS临时凭证与分片上传实现高效安全的文件传输,显著降低服务端负载与上传耗时,提升系统稳定性与用户体验。
|
12月前
|
安全 Linux iOS开发
Burp Suite Professional 2025.10 发布 - Web 应用安全、测试和扫描
Burp Suite Professional 2025.10 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
751 3
|
SQL 安全 PHP
PHP 自发布以来一直在 Web 开发领域占据重要地位,PHP 8 更是带来了属性、刚性类型等新特性。
【10月更文挑战第1天】PHP 自问世以来,凭借其易用性和灵活性,在 Web 开发领域迅速崛起。从简单的网页脚本语言逐步演进为支持面向对象编程的现代语言,尤其自 PHP 5.3 引入命名空间后,代码组织和维护变得更加高效。PHP 7 的性能优化和 PHP 8 的新特性(如属性和刚性类型)进一步巩固了其地位。框架如 Laravel、Symfony、Yii2 和 CodeIgniter 等简化了开发流程,提高了效率和安全性。
325 2
|
安全 Linux iOS开发
Burp Suite Professional 2025.9 发布 - Web 应用安全、测试和扫描
Burp Suite Professional 2025.9 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
767 0
Burp Suite Professional 2025.9 发布 - Web 应用安全、测试和扫描
|
安全 Linux iOS开发
Burp Suite Professional 2025.7 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
Burp Suite Professional 2025.7 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
950 0
Burp Suite Professional 2025.7 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
|
安全 Linux API
Burp Suite Professional 2025.4 发布 - Web 应用安全、测试和扫描
Burp Suite Professional 2025.4 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
658 6
Burp Suite Professional 2025.4 发布 - Web 应用安全、测试和扫描
|
存储 安全 JavaScript
Web Storage有哪些安全风险?
Web Storage有哪些安全风险?
|
人工智能 Linux iOS开发
Burp Suite Professional 2025.2 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
Burp Suite Professional 2025.2 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
1212 12
Burp Suite Professional 2025.2 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
|
缓存 安全 搜索推荐
阿里云先知安全沙龙(北京站) ——浅谈Web快速打点
信息收集是网络安全中的重要环节,常用工具如Hunter、Fofa和扫描工具可帮助全面了解目标系统的网络结构与潜在漏洞。遇到默认Nginx或Tomcat 404页面时,可通过扫路径、域名模糊测试、搜索引擎缓存等手段获取更多信息。AllIN工具(GitHub: P1-Team/AllIN)能高效扫描网站路径,发现敏感信息。漏洞利用则需充分准备,以应对突发情况,确保快速拿下目标站点。 简介:信息收集与漏洞利用是网络安全的两大关键步骤。通过多种工具和技术手段,安全人员可以全面了解目标系统,发现潜在漏洞,并制定有效的防御和攻击策略。
|
安全 大数据 数据挖掘
课时9:阿里云Web应用防火墙:全面保障网站的安全与可用性
阿里云Web应用防火墙(WAF)基于阿里巴巴十年攻防经验,提供全面的网站安全防护。它通过Web应用防护、CC攻击防护和业务风控,有效应对各类网络威胁,确保网站的安全与可用性。智能双引擎技术降低误报率,实时数据分析和虚拟补丁更新保障系统安全。WAF已成功护航多个重大活动,为企业提供高效、简便的安全解决方案。
650 0