当前位置:网站首页>matplotlib图表多曲线多纵轴绘制工具方法
matplotlib图表多曲线多纵轴绘制工具方法
2022-07-30 23:18:00 【 星云 】

matplotlib是常用的可视化库,画折线图只要把列表plot进去就可以轻松展示,这里只弄折线图,其它图暂时不管。同一图上不同曲线数值大小差太多就能绘制成地板和天花板还不能给人家量纲去了,所以不同曲线需要不同纵轴才能清晰看出细小波动~
matplotlib怎么作图,用一次搜一次每次只要import matplotlib就搜,写个常用工具别再搜了
环境
- Python:3.9.12
- matplotlib:3.5.2
代码
import random
from typing import Iterable
import matplotlib.pyplot as plt
from mpl_toolkits.axisartist.parasite_axes import HostAxes, ParasiteAxes
def hsv_to_rgb(h, s, v):
c = v * s
x = c * (1 - abs(int(h/60) % 2-1))
m = v - c
if 0 <= h < 60:
r, g, b = c, x, 0
elif 60 <= h < 120:
r, g, b = x, c, 0
elif 120 <= h < 180:
r, g, b = 0, c, x
elif 180 <= h < 240:
r, g, b = 0, x, c
elif 240 <= h < 300:
r, g, b = x, 0, c
elif 300 <= h < 360:
r, g, b = c, 0, x
r, g, b = r+m, g+m, b+m
return r, g, b
def plot(data: 'dict[str,Iterable]', axis_distance: int = 40, x_label: str = None, colors: Iterable = None, padding: float = .1):
"""多曲线展示 Parameters ---------- data : dict[str,Iterable] 待展示数据 axis_distance : int, optional 坐标轴间距, by default 40 x_label : str, optional 横轴标签, by default None colors : Iterable, optional 候选色彩,若色彩数量不足以显示曲线则分配不到色彩的曲线用随机颜色, by default None """
fig = plt.figure(1)
ax_cof = HostAxes(fig, [padding, padding, 1 - padding * 2, 1 - padding * 2])
ax_cof.axis['right'].set_visible(False)
ax_cof.axis['top'].set_visible(False)
color_list = [hsv_to_rgb(h, random.random()*.2 + .6, random.random()*.2 + .7) for h in range(0, 360, 30)]
random.shuffle(color_list)
color_list = [ele for ele in colors or []]+color_list
random_color_count = len(data.keys())-len(color_list)
if random_color_count > 0:
color_list += [hsv_to_rgb(random.randint(0, 360-1), random.random()*.2 + .6, random.random()*.2 + .7) for _ in range(random_color_count)]
color_list.reverse()
index = 0
for label, value in data.items():
color = color_list.pop()
if index:
axe = ParasiteAxes(ax_cof, sharex=ax_cof)
axe.set_ylabel(label)
ax_cof.parasites.append(axe)
axisline = axe.get_grid_helper().new_fixed_axis
key_name = '?'+label
axe.axis[key_name] = axisline(loc='right', axes=axe, offset=((index - 1) * axis_distance, 0))
axe.plot(value, label=label, color=color)
axe.axis[key_name].label.set_color(color)
axe.axis[key_name].major_ticks.set_color(color)
axe.axis[key_name].major_ticklabels.set_color(color)
axe.axis[key_name].line.set_color(color)
else:
if x_label:
ax_cof.set_xlabel(x_label)
ax_cof.set_ylabel(label)
ax_cof.plot(value, label=label, color=color)
index += 1
fig.add_axes(ax_cof)
ax_cof.legend()
plt.show()
边栏推荐
- 【LeetCode】55. 跳跃游戏 - Go 语言题解
- uniapp折叠框二级循环
- #Dasctf July Enabler WP
- 【2022-05-31】JS逆向之易企秀
- Compressing Deep Graph Neural Networks via Adversarial Knowledge Distillation
- “蔚来杯“2022牛客暑期多校训练营4 L.Black Hole 垃圾计算几何
- Chevrolet Trailblazer, the first choice for safety and warmth for your family travel
- Debezium报错系列之二十:task failed to create new topic.Ensure that the task is authorized to create topics
- DFS question list and template summary
- 2021GDCPC广东省大学生程序设计竞赛 B.Byfibonacci
猜你喜欢

软件测试三阶段,你在哪一步?

实验8(vlan实验)

递增三元组

The difference between ?? and ??= and ?. and || in JS

#Dasctf July Enabler WP

#yyds干货盘点# 面试必刷TOP101:判断链表中是否有环

MySQL索引常见面试题(2022版)

Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)

mysql中关于存储过程无法实现迁移复制表中数据问题

Py's pdpbox: a detailed introduction to pdpbox, installation, and case application
随机推荐
实验8(vlan实验)
# # yyds dry goods inventory interview will brush TOP101: to determine whether there is a part of the list
HF2022-EzPHP复现
MySQL的一个问题
数据清洗-使用es的ingest
Gxlcms audio novel system/novel listening system source code
Summary of BFS questions
ZZULIOJ:1120: 最值交换
2021GDCPC广东省大学生程序设计竞赛 B.Byfibonacci
StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?
编码与进制
Achievements of Science and Technology (31)
通过对抗性知识蒸馏压缩深度图神经网络
可视化工具Netron介绍
Successfully solved ImportError: always import the name '_validate_lengths'
微软商店出现【0x800706D9】解决方法
vscode上利用screen命令跑代码
Golang 切片删除指定元素的几种方法
反转链表-头插反转法
Flex布局使用