的用戶定義的列表我有繪製使用pyplot
圖形的量的函數。代碼在這裏:添加二次X軸座標
def plot_results(results, expers):
"""
type results: list[list[float]]
type expers: list[int]
"""
label_builder = lambda index: 'experiment ' + str(index + 1)
colors = ('green', 'blue')
x_indices = list(map(compute_filesize, list(range(np.shape(results)[1]))))
x_percents = list(map(compute_percent, list(range(np.shape(results)[1]))))
fig, ax1 = plt.subplots()
for i in range(expers):
ax1.plot(x_indices, results[i], color=colors[i], lw=2, label=label_builder(i))
ax1.legend()
plt.show()
對於expers
的每個值列出我的函數繪製圖表。 例如,if len(results) == len (expers) == 2
,我會得到這樣的曲線圖:
我需要創建二次X軸(類似於this,但它可以是X軸並且將位於圖的頂部)。 另一個區別是,我需要設置座標手動(例如ax2.set_coords(x_percents))的列表。
我使用ax2 = ax1.twinx()
創造了新的軸。然後,我使用ax2.set_xticks(x_percents)
建立座標列表。
但因爲每個x_percents[i] < x_indices[i]
的,我得到了這樣的畫面:
(你可以看到,新的軸的所有的座標都位於左下角)
哪有我改變我的功能,使新的X軸:
位於圖的頂側,
都有自己的刻度,即
x_percents
每一值對應於分散在整個間隔的results[i]
和x_percents
值?
你想其實這個其他軸上繪製數據,或者這是第二軸第一的另一種represantation? – MaxNoe
也'np.arange(results.shape [1])'會比更清潔和更快什麼喲正在做 – MaxNoe