# Show elevation profile from linestring's z values

I have linestring geometries with z values and I want to display them in a nice elevation profile (with x/z scaling functions, several profiles to overlay, and so on).

Is there any QGIS function or plugin for it?

I've seen tools for digitizing elevation profiles from DEM's, e.g. Profile tool. These tools have such a height profile display, but they seem to require the height digitization I don't need.

I could go to Excel, yes. But I'd like to stay inside QGIS in order to avoid another interface.

I found DataPlotly, which seems to be a powerful tool that plots basically every information that is inside layer geometries and / or attributes. The x/y plot option can be used to get an elevation profile. However, DataPlotly needs point geometries with z/m value pairs then.

So one needs to assign m-values based on distance to the line feature (e.g. using some LRS tools), then convert line vertices to points.

The result is fancy, but the way to achieve it is laborious. I consider now writing an own tool: Click on the line feature => process line to points with z/m => show the plot.

Thanks to @BERA's comment, I found Matplotlib, a very powerful plotting tool that comes with standard python distributions, so it is also available in QGIS.

Here's an example for a console script that plots a polyline's Z values (M values would work similar, btw):

``````import matplotlib.pyplot as plt
import numpy as np
from qgis._core import QgsVertexId

layer = iface.activeLayer()
lines = layer.selectedFeatures()
lineGeometries = [line.geometry().constGet() for line in lines]

distances = []
zValues = []

# get x and y values
for geo in lineGeometries:
for i in range(0, geo.vertexCount() - 1):
distances.append(geo.segmentLength(QgsVertexId(_vertex=i-1)))
zValues.append(geo[i].z())

distances = np.cumsum(distances)

# Plot
plt.plot(distances, zValues, 'g')
plt.xlabel("Distance")
plt.ylabel("Z Value")
plt.title("Z Value Plot")
plt.show()
``````

The function chooses the selected polyline(s) of the active layer. The resulting plot can be zoomed, stretched and panned by the control buttons of the plot window:

(The control buttons don't fit well well with QGIS dark theme, but that's a minor issue I'd say)

More customisation is possible, the `matplotlib` module offers huge possibilities I still need to explore.

yes, you can check the geoscience plugin and the feature 'Section Manager'