0
我試圖繪製一個在matplotlib上有兩個x軸的圖。但是,它不表現我將如何指望它在matplotlib上創建2個x軸不工作
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax1 = fig.add_subplot(111)
x = np.linspace(-10,10,1000)
y = np.sin(x*np.pi/2)
ax1.plot(x,y)
ax2 = ax1.twiny()
tick_locations = ax1.get_xticks() - 5.
tick_labels = ax1.get_xticks()
ax2.set_xticks(tick_locations[1:])
ax2.set_xticklabels(tick_labels[1:])
plt.show()
給出:
不應該AX2標籤寫着:[ -5. 0. 5. 10.]
在AX1位置[-10. -5. 0. 5.]
任何想法,爲什麼它的伸出他們?
基於卡拉加馬的迴應[這篇文章](http://stackoverflow.com/questions/10514315/how-to-add-a-second-x-axis-in-matplotlib)它似乎你應該能夠選擇任何勾號位置? – Ben
@Ben回答更新,使用'set_xlim' – zhangxaochen
看起來'set_xlim'需要在'set_xticks'之後。其他明智的'set_xticks'重新定義了限制。但是你是對的,那是問題所在。 – Ben