当前位置:网站首页>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()
边栏推荐
- [0x800706D9] solution appears in Microsoft Store
- Week 19 Progress (Understanding IoT Basics)
- Apache Doris series: In-depth understanding of real-time analytical database Apache Doris
- vscode上利用screen命令跑代码
- oracle数据库版本问题咨询(就是对比从数据库查询出来的版本,和docker里面的oracle版本)?
- [MySQL] DQL related operations
- 2022牛客暑期多校训练营1 J Serval and Essay
- [MySQL] Related operations on databases and tables in MySQL
- grub learning
- Successfully solved ImportError: always import the name '_validate_lengths'
猜你喜欢
Lambda表达式
【MySQL】Mysql事务以及权限管理
【LeetCode】64. 最小路径和 - Go 语言题解
A detailed explanation: SRv6 Policy model, calculation and drainage
# Dasctf 7月赋能赛 WP
微软商店出现【0x800706D9】解决方法
Mysql进阶优化篇01——四万字详解数据库性能分析工具(深入、全面、详细,收藏备用)
抽象类和接口(学习笔记)
Apache Doris series: In-depth understanding of real-time analytical database Apache Doris
VS2017 compile Tars test project
随机推荐
Ningbo Zhongning Pawn will transfer 29.5% of the equity for 2.8338 million yuan, and the owner's equity in 2021 will be 9.6875 million yuan
(WebFlux)001、如何自定义注解实现功能
Successfully resolved ModuleNotFoundError: No module named 'Image'
2021GDCPC广东省大学生程序设计竞赛 H.History
ZZULIOJ:1119: 数列有序
Apache Doris系列之:安装与部署详细步骤
Go语学习笔记 - gorm使用 - gorm处理错误 Web框架Gin(十)
【Untitled】
第一节 zadig 入门
通过对抗性知识蒸馏压缩深度图神经网络
Soft Exam Summary
2022 China Logistics Industry Conference and Entrepreneur Summit Forum will be held in Hangzhou!
leetcode 406. Queue Reconstruction by Height 根据身高重建队列(中等)
反转链表-就地逆置法
HF2022-EzPHP reproduction
PS Basic Learning (1)
reindex win10
软件测试三阶段,你在哪一步?
HCIP Day 15 Notes
MySQL索引常见面试题(2022版)