Matplotlib中的非线性第二轴 [英] Non-linear Second Axis in Matplotlib

查看:110
本文介绍了Matplotlib中的非线性第二轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如果没有分析公式,是否可以在Matplotlib中添加第二个非线性x轴.或者简化一下,是否可以为原始x轴上的每个数字创建一个不同的标签.下图说明了我要寻找的东西.不幸的是,之前曾有人问过类似的问题,但未得到答复

I would like to know if there is a way to add second non-linear x-axis in Matplotlib if one doesnt have any analytical formula. Or to simplify if there is a way to create a distinct label for each number in the original x-axis. The following figure explains what I am looking for. Unfortunately similar question has been asked before but left unanswered.

推荐答案

由于该问题明确要求两个轴之间的任意关系(或拒绝澄清),因此以下代码绘制了一个任意关系.

Since the question explicitely asks for arbitrary relation between the two axes (or refuses to clarify), here is a code that plots an arbitrary relation.

import matplotlib.pyplot as plt
import numpy as np

a, b = (2*np.random.rand(2)-1)*np.random.randint(1,500, size=2)
time = lambda T: a*T+b
Temp = lambda t: (t-b)/a

T = np.linspace(0, 100, 301)
y = T**2

fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.25)

ax.set_xlabel("Temperature")
ax.plot(T,y)

ax2 = ax.secondary_xaxis(-0.2, functions=(time, Temp))
ax2.set_xlabel("Time")

plt.show()

输出看起来可能像这样,但是可能有所不同,因为该关系是任意的,并且可能会根据所取的随机数而发生变化.

The output may look like this, but may be different, because the relation is arbitrary and subject to change depending on the random numbers taken.

这篇关于Matplotlib中的非线性第二轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆