我想要做的就是在一個插曲一個2x2圖。然後,對於每個圖形,我將使用兩個y軸。因此,我爲每個圖使用了twinx()方法。我在圖中看到的問題是,它沒有顯示第一行的xlabel和xticks。對於第二排,一切都很好。我已經在圖中以紅色字體指定了問題(「無xlabel和xticks !!!」)。matplotlib - 在subploted數字沒有xlabel和twinx軸xticks
每個圖都具有其自己的x軸和y軸,並且沒有共享在於。
我已經調整了這個代碼了很多,縮小問題的製造者。這是因爲我在上面的行中使用了twinx()。如果我嘗試刪除輔助y軸,則所有內容都會恢復正常,上部行的xlabel和ylabel會正確顯示。
我不知道是什麼問題!
這是我工作的代碼。
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from io import StringIO
s = StringIO(u""" amount price
A 40929 4066443
B 93904 9611272
C 188349 19360005
D 248438 24335536
E 205622 18888604
F 140173 12580900
G 76243 6751731
H 36859 3418329
I 29304 2758928
J 39768 3201269
K 30350 2867059""")
df = pd.read_csv(s, index_col=0, delimiter=' ', skipinitialspace=True)
fig = plt.figure()
ax_2 = fig.add_subplot(222, sharex=None, sharey=None)
ax_22 = ax_2.twinx()
ax_2.plot([1, 3, 5, 7, 9])
ax_22.plot([1.0/x for x in [1, 3, 5, 7, 9]])
ax_2.set_xlabel("AX2 X Lablel")
ax_2.set_ylabel("AX2 Y Lablel")
ax_22.set_ylabel("AX2_Twin Y Lablel")
ax_2 = fig.add_subplot(223, sharex=None, sharey=None)
ax_22 = ax_2.twinx()
ax_2.plot([100, 300, 500, 700, 900])
ax_22.plot([x*x for x in [100, 300, 500, 700, 900]])
ax_2.set_xlabel("AX3 X Lablel")
ax_2.set_ylabel("AX3 Y Lablel")
ax_22.set_ylabel("AX3_Twin Y Lablel")
ax_2 = fig.add_subplot(224, sharex=None, sharey=None)
ax_22 = ax_2.twinx()
ax_2.set_xlabel("AX4 X Lablel")
ax_2.set_ylabel("AX4 Y Lablel")
ax_22.set_ylabel("AX4_Twin Y Lablel")
ax = fig.add_subplot(221, sharex=None, sharey=None)
ax2 = ax.twinx()
width = 0.4
df.amount.plot(kind='bar', color='red', ax=ax, width=width, position=1, sharex=False, sharey=False)
df.price.plot(kind='bar', color='blue', ax=ax2, width=width, position=0, sharex=False, sharey=False)
ax.set_xlabel("Alphabets")
ax.set_ylabel('Amount')
ax2.set_ylabel('Price')
plt.subplots_adjust(wspace=0.8, hspace=0.8)
plt.savefig("t1.png", dpi=300)
plt.show()
它會產生如下圖所示:
編輯:
感謝您的答案。但是,在繪製圖表中使用「熊貓」時,我的問題仍然存在。我開了一個新問題。請看一看吧:
matplotlib - pandas - no xlabel and xticks for twinx axes in subploted figures
謝謝Oniow。即使你提到的變化,圖中沒有變化! 我使用Ubuntu 14.04 64位在我的Mac上運行我的腳本。 Python:2.7.11(與GCC 4.4.7編譯) Matplotlib:1.5.1 而且,我使用Anaconda 2.4.0作爲我的Python包的包管理器。 – Millad
您是否以任何方式更改我的代碼?我問這是因爲我需要生成圖表,我想知道我是否犯了一個錯誤。我提到的上述系統配置是否與您的不同?謝謝你的時間。 – Millad
Hello Millad - 我能夠使用Python 3.4和Matplotlib 1.5.0製作劇情,並使用Anaconda。我沒有改變你的代碼來獲得所需的結果。它可能是一個python 3與2的東西 - 但我懷疑它。你使用什麼接口?你是直接保存數字還是將它輸出到Ipython控制檯? – Oniow