2013-07-16 301 views
3

我想创建一个具有两个x轴和一个y轴的特殊绘图。底部X轴的值增加,顶部X轴的值减小。我有一个x-y对,为此我想绘制y轴上的一个x轴和顶部x'不同比例轴:(x' = f(x))Matplotlib:绘制x/y坐标,带两个具有倒数标度的x轴

在我的情况下,xx'之间的转换是x' = c/x,其中c是一个常数。我找到一个例子here,它处理这种转换。不幸的是,这个例子不适用于我(没有错误消息,输出只是没有转换)。

我使用python 3.3matplotlib 1.3.0rc4 (numpy 1.7.1)

有谁知道一个方便的方式与matplotlib做到这一点?

编辑: 我发现计算器(https://stackoverflow.com/a/10517481/2586950)的答案,帮助我得到想要的情节。只要我可以发布图片(由于声望限制),我会在这里发布答案,如果任何人有兴趣。

回答

1

以下代码的输出对我来说是令人满意的 - 除非有一些更方便的方法,我坚持这一点。

import matplotlib.pyplot as plt 
import numpy as np 

plt.plot([1,2,5,4]) 
ax1 = plt.gca() 
ax2 = ax1.twiny() 

new_tick_locations = np.array([.1, .3, .5, .7,.9]) # Choosing the new tick locations 
inv = ax1.transData.inverted() 
x = [] 

for each in new_tick_locations: 
    print(each) 
    a = inv.transform(ax1.transAxes.transform([each,1])) # Convert axes-x-coordinates to data-x-coordinates 
    x.append(a[0]) 

c = 2 
x = np.array(x) 
def tick_function(X): 
    V = c/X 
    return ["%.1f" % z for z in V] 
ax2.set_xticks(new_tick_locations) # Set tick-positions on the second x-axes 
ax2.set_xticklabels(tick_function(x)) # Convert the Data-x-coordinates of the first x-axes to the Desired x', with the tick_function(X) 

A possible way to get to the desired plot.

1

我不知道如果这是你在找什么,但在这里它是无论如何:

import pylab as py 
x = py.linspace(0,10) 
y = py.sin(x) 
c = 2.0 

# First plot 
ax1 = py.subplot(111) 
ax1.plot(x,y , "k") 
ax1.set_xlabel("x") 

# Second plot 
ax2 = ax1.twiny() 
ax2.plot(x/c, y, "--r") 
ax2.set_xlabel("x'", color='r') 
for tl in ax2.get_xticklabels(): 
    tl.set_color('r') 

example

我猜测这是你的

是什么意思我有一个xy对,为此我想在一个x轴上绘制y,并在一个x'轴下绘制不同的缩放图。

但是,如果我错了,我很抱歉。

+0

嘿,哇,快回答。原则上最终图应该看起来像这样,唯一的问题是:x'= c/x,它是一个反比关系 - 如果我通过在ax2.plot(x/c,y, “--r”)这两个函数不再一致。 –

+0

当然,他们不是在绘制相同的y数据,而是完全不同的规模。所以它仍然是一个sin函数,但是延伸为x-> inf。你在x = 0时也会遇到问题。尝试用铅笔和纸画出你正在寻找的东西。 – Greg

+1

我认为可以安全地说,大多数在这里发布的人都知道函数如何依赖于其参数并将其绘制在轴上。我很难让自己清楚,对不起。正如在编辑中提到的那样,我找到了答案。我会尽快发布,澄清问题。 –

相关问题