簡體   English   中英

Python Matplotlib Y-Axis在Plot右側打勾

[英]Python Matplotlib Y-Axis ticks on Right Side of Plot

我有一個簡單的線圖,需要將y軸刻度從繪圖的(默認)左側移動到右側。 有關如何做到這一點的任何想法?

使用ax.yaxis.tick_right()

例如:

from matplotlib import pyplot as plt

f = plt.figure()
ax = f.add_subplot(111)
ax.yaxis.tick_right()
plt.plot([2,3,4,5])
plt.show()

在此輸入圖像描述

對於正確的標簽,請使用ax.yaxis.set_label_position("right") ,即:

f = plt.figure()
ax = f.add_subplot(111)
ax.yaxis.tick_right()
ax.yaxis.set_label_position("right")
plt.plot([2,3,4,5])
ax.set_xlabel("$x$ /mm")
ax.set_ylabel("$y$ /mm")
plt.show()

joaquin的回答是有效的,但是具有從軸的左側去除刻度的副作用。 為了解決這個問題,跟進tick_right()與一個呼叫set_ticks_position('both') 修改后的例子:

from matplotlib import pyplot as plt

f = plt.figure()
ax = f.add_subplot(111)
ax.yaxis.tick_right()
ax.yaxis.set_ticks_position('both')
plt.plot([2,3,4,5])
plt.show()

結果是兩側都有刻度的圖,但右側是刻度標簽。

在此輸入圖像描述

只是有人問(就像我做的那樣),當使用subplot2grid時,這也是可能的。 例如:

import matplotlib.pyplot as plt
plt.subplot2grid((3,2), (0,1), rowspan=3)
plt.plot([2,3,4,5])
plt.tick_params(axis='y', which='both', labelleft='off', labelright='on')
plt.show()

它會顯示:

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM