Matplotlib Connect Scatterplot Points with Line - Python

Matplotlib connect scatterplot points with line - Python

I think @Evert has the right answer:

plt.scatter(dates,values)
plt.plot(dates, values)
plt.show()

Which is pretty much the same as

plt.plot(dates, values, '-o')
plt.show()

You can replace -o with another suitable format string as described in the documentation.
You can also split the choices of line and marker styles using the linestyle= and marker= keyword arguments.

Plot Numbers Instead of Points Using Matplotlib Scatterplot

You can just plot text anywhere you want, but you have to set the limits of the image manually by calling .xlim() and .ylim()

import numpy as np
import matplotlib.pyplot as plt

centroidCoords = [(7.210123936676805, -0.0014481952154823),
(5.817327756517152, -1.0513260084561042),
(5.603133733696165, -2.7765635631249412),
(4.500525247710033, -0.8659667639805515),
(3.9999999999880367, -2.089987631283091)]

textCoord = [f"{x:.2f} {y:.2f}" for x, y in centroidCoords]
x, y = np.array(centroidCoords).T

for col, row, t in zip(x, y, textCoord):
plt.text(col, row, t, fontsize=10, color="red")
plt.xlim(min(x)-1, max(x)+1)
plt.ylim(min(y)-1, max(y)+1)
plt.show()

Sample Image

Python Scatter Plot: Cannot connect points

Scatter plot just plots the dots, if you want to connect them. You can use
plot() function with same arguments of scatter

plt.scatter(x,y)
plt.plot(x, y)
plt.show()


Related Topics



Leave a reply



Submit