0

I have something like the following dataframe where each record is a point along a line. Fields include distance from the line's beginning and point elevation.

>>> 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
>>> 

I want to create a plot that shows the elevation profile. Both distance and elevation are scaled in feet and I want it so increments of x and y have the same length (so the plot more realistically shows the elevation profile).

>>> 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

Now that scaling is correct (a foot is represented by the same distance in both the x and y axis) how can I set the y limits so that they better suit the data (from -5 to 25 for example)?

1 Answer 1

2

If you use the OO approach, then you can use 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

Sign up to request clarification or add additional context in comments.

2 Comments

thank you that's great. what does "the OO approach" stand for?
object-orientated. I.e. making a figure and axes, and then calling ax.plot, ax.set_ylim, fig.savefig etc. rather than plt.plot, plt.ylim, etc.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.