[英]How can I plot from a maximum range to a minimum range in Python
我在嘗試用python繪制圖形以完成我在大學時的作業時遇到了一些麻煩。 我想在x軸上繪制從最大范圍到最小范圍的圖形。 我的代碼是下一個:
import numpy as np
import matplotlib.pyplot as plt
# function that plots the cummulative histogram
def plotFunction( array , numberBins ):
# Array of elements that will be plotted on log scale
elements = array
# evaluate the histogram
values, base = np.histogram(elements, bins= len( numberBins ) )
#evaluate the cumulative
cumulative = np.cumsum(values)
# plot the cumulative function
plt.plot( cumulative , base[:-1] , c='blue')
plt.show()
我想將軸從200改為20。
使用軸的xlim函數或set_xlim方法:
plt.xlim(200, 20)
一種方法是手動設置限制,如@tillsten所述。 這是簡單,直接的方法,如果您只是制作一個靜態圖形,這就是您想要的。
您甚至可以執行plt.xlim(plt.xlim()[::-1])
以避免輸入特定范圍。
但是,這會關閉自動縮放的副作用。 如果要(可能是交互式地)向圖添加更多內容,則可能需要使用ax.invert_xaxis()
。
在您的情況下,您是通過pyplot
調用plot方法(而不是作為軸的方法),因此您需要首先獲取pyplot
實例。 例如
plt.gca().invert_xaxis()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.