3

I want to create a special plot with two x axis and one y axis. The bottom X axis increases in value, and the top X axis decreases in value. I have an x-y pair, for which I want to plot y over one x-axes and on the top x' axes with different scale: (x' = f(x)).

In my case, the conversion between x and x' is x' = c/x, where c is a constant. I found an example here, which deals with transformations of this kind. Unfortunately this example doesn't work for me (no error message, the output is just not transformed).

I am using python 3.3 and matplotlib 1.3.0rc4 (numpy 1.7.1)

Does anybody know a convenient way to do this with matplotlib?

EDIT: I found an answer on stackoverflow (https://stackoverflow.com/a/10517481/2586950) which helped me to get to the desired plot. As soon as I can post Images (due to Reputation-Limit), I will post the answer here, if anyone is interested.

4

2 回答 2

2

我不确定这是否是您正在寻找的,但无论如何它都在这里:

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')

例子

我猜这就是你的意思

我有一个 xy 对,我想在一个 x 轴上和一个 x' 轴下以不同的比例绘制 y。

但如果我错了,我道歉。

于 2013-07-16T11:39:25.197 回答
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)

一种可能的方式来获得所需的情节。

于 2013-07-17T07:48:37.747 回答