3

2 つの x 軸と 1 つの y 軸を持つ特別なプロットを作成したいと考えています。下の X 軸の値が増加し、上の X 軸の値が減少します。x-yy を 1 つの x 軸と上のx'軸に異なるスケールでプロットしたいペアがあります(x' = f(x))

x私の場合、との間の変換x'は ですx' = c/x。c は定数です。この種の変換を扱う例hereを見つけました。残念ながら、この例はうまくいきません (エラー メッセージは表示されず、出力は変換されません)。

私は使用python 3.3していますmatplotlib 1.3.0rc4 (numpy 1.7.1)

matplotlib でこれを行う便利な方法を知っている人はいますか?

編集:スタックオーバーフロー ( https://stackoverflow.com/a/10517481/2586950 ) で 回答を見つけたので、目的のプロットにたどり着くことができました。画像を投稿できるようになり次第 (評判の制限により)、興味がある場合はここに回答を投稿します。

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 ペアがあり、y を 1 つの x 軸と 1 つの x' 軸の下に異なるスケーリングでプロットしたいと考えています。

でも間違っていたらごめんなさい。

于 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 に答える