如何在Python中从最大范围打印到最小范围
我在大学里做作业时,在用python绘制一个图形时遇到了一些麻烦。我想在x轴上绘制一个从最大范围到最小范围的图形。我的代码是下一个:如何在Python中从最大范围打印到最小范围,python,numpy,plot,histogram,Python,Numpy,Plot,Histogram,我在大学里做作业时,在用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 plott
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方法,因此需要首先获取axes实例。例如
plt.gca().invert_xaxis()