当前位置:网站首页>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()
边栏推荐
猜你喜欢
CPM:A large-scale generative chinese pre-trained lanuage model
【LeetCode】42. 接雨水 - Go 语言题解
EasyExcel comprehensive course combat
Abstract classes and interfaces (study notes)
2022 China Logistics Industry Conference and Entrepreneur Summit Forum will be held in Hangzhou!
2021GDCPC Guangdong University Student Programming Competition H.History
打动中产精英群体,全新红旗H5用产品力跑赢需求
Week 19 Progress (Understanding IoT Basics)
[MySQL] DQL related operations
【LeetCode】55. 跳跃游戏 - Go 语言题解
随机推荐
MySql统计函数COUNT详解
2022.7.30
StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?
ZZULIOJ: 1120: the most value to exchange
grub 学习
【MySQL】MySQL中对数据库及表的相关操作
Manually set transaction commit in mysql
# Dasctf 7月赋能赛 WP
PhpMetrics usage
一文详解:SRv6 Policy模型、算路及引流
【Untitled】
抽象类和接口(学习笔记)
Summary of BFS questions
2022 Nioke Summer Multi-School Training Camp 1 J Serval and Essay
leetcode(刷题篇13)
Chapter 8 Intermediate Shell Tools II
10 个关于自动化发布管理的好处
IJCAI2022 Tutorial | Spoken Language Comprehension: Recent Advances and New Fields
电脑快捷方式图标变白解决方案
【LeetCode】70. 爬楼梯 - Go 语言题解