当前位置:网站首页>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()
边栏推荐
- vscode上利用screen命令跑代码
- Debezium报错系列之二十:task failed to create new topic.Ensure that the task is authorized to create topics
- 二进制序列
- MySQL的一个问题
- The problem of sticky packets in tcp protocol transmission
- Excel basic study notes
- (WebFlux)001、如何自定义注解实现功能
- Debezium error series 20: task failed to create new topic. Ensure that the task is authorized to create topics
- Gxlcms audio novel system/novel listening system source code
- Achievements of Science and Technology (31)
猜你喜欢

Gxlcms audio novel system/novel listening system source code

2sk2225 Substitute 3A/1500V Chinese Documentation【PDF Data Book】

Apache Doris系列之:深入认识实时分析型数据库Apache Doris

MySQL进阶sql性能分析

阿里云视频点播+项目实战

可视化工具Netron介绍

【MySQL】DQL相关操作

通过对抗性知识蒸馏压缩深度图神经网络

Chapter 8 Intermediate Shell Tools II

# # yyds dry goods inventory interview will brush TOP101: to determine whether there is a part of the list
随机推荐
A detailed explanation: SRv6 Policy model, calculation and drainage
2022.7.27
2022.7.30
【飞控开发基础教程10】疯壳·开源编队无人机-PID 基础原理
二进制序列
#yyds干货盘点# 面试必刷TOP101:判断链表中是否有环
# Dasctf 7月赋能赛 WP
Successfully solved ImportError: always import the name '_validate_lengths'
HCIP Day 15 Notes
解决一个Mysql的utf8编码导致的问题
Successfully resolved ModuleNotFoundError: No module named 'Image'
IJCAI2022 Tutorial | Spoken Language Comprehension: Recent Advances and New Fields
反转链表-头插反转法
$\text{ARC 145}$
Lambda表达式
软件测试三阶段,你在哪一步?
【Untitled】
2sk2225 Substitute 3A/1500V Chinese Documentation【PDF Data Book】
tcp协议传输中的粘包问题
只会纯硬件,让我有点慌