2014-09-10 72 views
0

我在第二個x軸上繪製了一個圖。如何將第二個軸的零點位置轉換爲特定值?我想第二個X軸的0位置與我的主要X軸的值2.13相對應。對於我使用的第二個x軸設置第二個x軸零位

fig = plt.figure() 
ax1 = fig.add_subplot(111) 
ax2 = ax1.twiny() 

我從名爲epsilon的列表中讀取第二個x軸的值。

def tick_function(X): 
    return ["%.4f" % z for z in X] 

ax2.set_xticklabels(tick_function(epsilon)) 

回答

0

你必須設置綁定爲第二個X軸:

enter image description here

代碼:

#!/usr/bin/python3 

import matplotlib.pyplot as plt 

fig = plt.figure() 
ax1 = fig.add_subplot(111) 

x1 = [2.13, 2.135, 2.139, 2.14, 2.143] 
y1 = range(len(x1)) 

x2 = [0, 100, 200, 350, 450] 

ax1 = fig.add_subplot(111) 
ax1.plot(x1, y1) 
ax1.grid(True) 

ax2 = ax1.twiny() 

ax2.set_xticks(x1) 
ax2.set_xbound(ax1.get_xbound()) 
ax2.set_xticklabels(x2) 

fig.savefig("15.png") 
+0

是的,這是謝謝你,但幅度上頂部x軸在我的情況下與底部完全不同。最下面的是2.1 - 2.4,在頂部,我想在0到450範圍內的特定位置上只顯示5個值(在特定的非線性位置,比如在2.13值爲0,在2.135值爲100,在2.139 200,在2.14 350和在2.143450)。這有點複雜。 – user3041107 2014-09-10 11:46:00

+0

@ user3041107:現在它更清晰。您應該真的在原始文章中提供完整的數據。 – Adobe 2014-09-10 12:00:10

+0

好的,非常感謝,這是我需要的。很抱歉對於這個誤會。 – user3041107 2014-09-10 12:16:22