2015-12-10 69 views
0

我有类似于以下数据框的内容,其中每条记录都是沿着一条线的点。字段包括线路起点和点高程的距离。Matplotlib pyplot设置重新缩放之后的轴限制等于

>>> import pandas as pd 
>>> import matplotlib.pyplot as plt 
>>> 
>>> df=pd.DataFrame() 
>>> df['Distance']=[5,100,200,300,350,370,400] 
>>> df['Elevation']=[0,5,10,5,10,15,20] 
>>> df 
    Distance Elevation 
0   5   0 
1  100   5 
2  200   10 
3  300   5 
4  350   10 
5  370   15 
6  400   20 
>>> 

我想创建一个显示高程剖面图。距离和高程都以英尺为单位进行缩放,我希望它和x和y的增量具有相同的长度(因此,绘图更逼真地显示了高程配置文件)。

>>> plt.plot(df['Distance'],df['Elevation']) 
[<matplotlib.lines.Line2D object at 0x0A1C7EB0>] 
>>> plt.axis('equal') 
(0.0, 400.0, 0.0, 20.0) 
>>> plt.show() 
>>> 

enter image description here

现在缩放正确的(一个脚通过在x和y轴的两个相同的距离来表示)如何设置在y限制,使得它们更好地满足的数据(从例如-5到25)?

回答

1

如果使用面向对象方法,那么你可以使用ax.set_ylim(-5,25)

import pandas as pd 
import matplotlib.pyplot as plt 

df=pd.DataFrame() 
df['Distance']=[5,100,200,300,350,370,400] 
df['Elevation']=[0,5,10,5,10,15,20] 

fig = plt.figure() 
ax = fig.add_subplot(111, aspect='equal') 
ax.plot(df['Distance'],df['Elevation']) 
ax.set_ylim(-5,25) 

enter image description here

+0

感谢你,是伟大的。 “OO方法”代表什么? – AJG519

+0

面向对象。即制作图形和坐标轴,然后调用'ax.plot','ax.set_ylim','fig.savefig'等,而不是'plt.plot','plt.ylim'等。 – tom

+0

合理 - 谢谢 – AJG519

相关问题