IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> python plt 画图 自用 -> 正文阅读

[Python知识库]python plt 画图 自用

常用操作

基本

  • 设置x/y轴的名字:plt.xlabel,ax.set_xlabel。labelpad可以调整文字和坐标轴的距离
  • 设置坐标轴刻度:plt.xticks(x,x_自定义), ax.set_xticks。rotation可以调整文字的角度
  • 设置title:plt.title(), ax.set_title()
  • 绘制多个图:plt.subplot(122)
  • 检查字体
import matplotlib as mpl
mpl.font_manager.fontManager.ttflist
  • 保存图片
    ax.get_figure ().savefig(‘name’,dpi=400)
    plt.savefig(‘name’,dpi=400)
  • 显示中文
import matplotlib.pyplot as plt
# 支持中文
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
  • 设置边框的可见度
ax=plt.gca()  #gca:get current axis得到当前轴
#设置图片的右边框和上边框为不显示
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
  • 开始设置图像的大小plt.figure(figsize=(10, 5), dpi=400)
  • 设置图注:plt.legend() 在画图的时候每个图加一个label就可以
    另一种方式
line1, = plt.plot(xxx, label='女生购物欲望')
line2, = plt.plot(xxx, label='男生购物欲望')

plt.legend(handles=[line1, line2], labels=['girl购物欲望','boy购物欲望'], loc='best',title="图例标题")

颜色 colormap

详见https://blog.csdn.net/sinat_32570141/article/details/105226330

各种图

饼状图

参考https://blog.csdn.net/weixin_46649052/article/details/115321326

import matplotlib.pyplot as plt

labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'  # 定义标签
sizes = [15, 30, 45, 10]  # 每一块的比例
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']  # 每一块的颜色
explode = (0, 0.1, 0, 0)  # 突出显示,这里仅仅突出显示第二块(即'Hogs')

plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90)
plt.axis('equal')  # 显示为圆(避免比例压缩为椭圆)
plt.show()

折线图

参考https://zhuanlan.zhihu.com/p/110656183

import matplotlib.pyplot as plt
n = [0, 1, 2, 3, 4]
m = [2, 3, -1, 1, -2]
plt.plot(n, m, 
         color = 'k',
         linestyle = '-.',
         linewidth = 3,
         marker = 'p',
         markersize = 15,
         markeredgecolor = 'b',
         markerfacecolor = 'r')
plt.show()

柱状图

参考https://zhuanlan.zhihu.com/p/25128216

import numpy as np
import matplotlib.pyplot as plt

size = 5
x = np.arange(size)
a = np.random.random(size)
b = np.random.random(size)
c = np.random.random(size)

total_width, n = 0.8, 3
width = total_width / n
x = x - (total_width - width) / 2

plt.bar(x, a,  width=width, label='a')
plt.bar(x + width, b, width=width, label='b')
plt.bar(x + 2 * width, c, width=width, label='c')
plt.legend()
plt.show()

横过来的柱状图

import numpy as np
import matplotlib.pyplot as plt

a = np.array([5, 20, 15, 25, 10])
b = np.array([10, 15, 20, 15, 5])

plt.barh(range(len(a)), a)
plt.barh(range(len(b)), -b)
plt.show()

极坐标柱状图

import numpy as np
import matplotlib.pyplot as plt

N = 8 # 分成8份
theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False)
radii = np.random.randint(3,15,size = N)
width = np.pi / 4
colors = np.random.rand(8,3) # 随机?成颜?
ax = plt.subplot(111, projection='polar') # polar表示极坐标
ax.bar(
    x=theta,       # 柱子的位置
    height=radii, # 柱子的高度
    width=width,    # 柱子的宽度
    bottom=bottom,  # 开始画的位置
    linewidth=2,    # 线段宽度
    edgecolor='white',# 用白色分开
    color=colors      # 每个柱子的颜色
)

箱线图

别人博客学的骚操作

参考https://www.cnblogs.com/Big-Big-Watermelon/p/14052165.html

marker

普通marker
在这里插入图片描述
数学符号marker

可以显示的形状    marker名称
?   \varpi
?   \varrho
?   \varsigma
?   \vartheta
ξ   \xi
ζ   \zeta
Δ   \Delta
Γ   \Gamma
Λ   \Lambda
Ω   \Omega
Φ   \Phi
Π   \Pi
Ψ   \Psi
Σ   \Sigma
Θ   \Theta
Υ   \Upsilon
Ξ   \Xi
?   \mho
?   \nabla
?   \aleph
?   \beth
?   \daleth
?   \gimel
/   /
[   [
?   \Downarrow
?   \Uparrow
‖   \Vert
↓   \downarrow
?   \langle
?   \lceil
?   \lfloor
?   \llcorner
?   \lrcorner
?   \rangle
?   \rceil
?   \rfloor
?   \ulcorner
↑   \uparrow
?   \urcorner
\vert
{   \{
\|
}   \}
]   ]
|
?   \bigcap
?   \bigcup
?   \bigodot
?   \bigoplus
?   \bigotimes
?   \biguplus
?   \bigvee
?   \bigwedge
?   \coprod
∫   \int
∮   \oint
∏   \prod
∑   \sum

自定义marker

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']  # 用于显示中文
plt.rcParams['axes.unicode_minus'] = False  # 用于显示中文
plt.figure(dpi=200)
#常规marker使用
plt.plot([1,2,3],[1,2,3],marker=4, markersize=15, color='lightblue',label='常规marker')
plt.plot([1.8,2.8,3.8],[1,2,3],marker='2', markersize=15, color='#ec2d7a',label='常规marker')

#非常规marker使用
#注意使用两个$符号包围名称
plt.plot([1,2,3],[4,5,6],marker='$\circledR$', markersize=15, color='r', alpha = 0.5 ,label='非常规marker')
plt.plot([1.5,2.5,3.5],[1.25,2.1,6.5],marker='$\heartsuit$', markersize=15, color='#f19790', alpha=0.5,label='非常规marker')
plt.plot([1,2,3],[2.5,6.2,8],marker='$\clubsuit$', markersize=15, color='g', alpha=0.5,label='非常规marker')

#自定义marker
plt.plot([1.2,2.2,3.2],[1,2,3],marker='$666$', markersize=15, color='#2d0c13',label='自定义marker')
plt.legend(loc='upper left')
for i in ['top','right']:
    plt.gca().spines[i].set_visible(False)

线的形状

字符型linestyle

linestyle_str = [
('solid', 'solid'), # Same as (0, ()) or '-';solid’, (0, ()) , '-'三种都代表实线。
('dotted', 'dotted'), # Same as (0, (1, 1)) or '.'
('dashed', 'dashed'), # Same as '--'
('dashdot', 'dashdot')] # Same as '-.'

元组型linestyle
可以通过修改元组中的数字来呈现出不同的线型,因此可以构造出无数种线型。

linestyle_tuple = [
('loosely dotted', (0, (1, 10))),
('dotted', (0, (1, 1))),
('densely dotted', (0, (1, 2))), ('loosely dashed', (0, (5, 10))),
('dashed', (0, (5, 5))),
('densely dashed', (0, (5, 1))), ('loosely dashdotted', (0, (3, 10, 1, 10))),
('dashdotted', (0, (3, 5, 1, 5))),
('densely dashdotted', (0, (3, 1, 1, 1))), ('dashdotdotted', (0, (3, 5, 1, 5, 1, 5))),
('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))),
('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))]

线型使用代码:

import matplotlib.pyplot as plt
plt.figure(dpi=120)
#字符型linestyle使用方法
plt.plot([1,2,3],[1,2,13],linestyle='dotted', color='#1661ab', linewidth=5, label='字符型线型:dotted')

#元组型lintstyle使用方法 
plt.plot([0.8,0.9,1.5],[0.8,0.9,21.5],linestyle=(0,(3, 1, 1, 1, 1, 1)), color='#ec2d7a', linewidth=5, label='元组型线型:(0,(3, 1, 1, 1, 1, 1)')

for i in ['top','right']:
    plt.gca().spines[i].set_visible(False)
    
#自定义inestyle  
plt.plot([1.5,2.5,3.5],[1,2,13],linestyle=(0,(1,2,3,4,2,2)), color='black', linewidth=5, label='自定义线型:(0,(1,2,3,4,2,2)))')
plt.plot([2.5,3.5,4.5],[1,2,13],linestyle=(2,(1,2,3,4,2,2)), color='g', linewidth=5, label='自定义线型:(1,(1,2,3,4,2,2)))')
plt.legend()

在这里插入图片描述
不得不说这哥们写的真好

常用颜色

图片都是我从人家博客复制来的,csdn这个beyond自己打了水印
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
装了seaborn扩展的话,在字典seaborn.xkcd_rgb中包含所有的xkcd crowdsourced color names。
在这里插入图片描述

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2022-05-18 17:35:34  更:2022-05-18 17:37:31 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/18 13:24:53-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码