0

I am trying to plot a point to point line plot in python. My data is in a pandas dataframe as below..

df = pd.DataFrame({
'x_coordinate': [0, 0, 0, 0, 1, 1,-1,-1,-2,0],
'y_coordinate': [0, 2, 1, 3,  3, 1,1,-2,2,-1],
})
print(df)

      x_coordinate  y_coordinate
   0             0             0
   1             0             2
   2             0             1
   3             0             3
   4             1             3
   5             1             1
   6            -1             1
   7            -1            -2
   8            -2             2
   9             0            -1

when I plot this, it is joining from point to point as in the order in the df.

df.plot('x_coordinate','y_coordinate')

enter image description here

But, is there a way, I can plot an order number next to it ? I mean the order it is travelling. Say 1 for the first connection from (0,0) to (0,2) and 2 from (0,2) to (0,1) and so on ?

Mr. T
  • 11,960
  • 10
  • 32
  • 54
Johnson Francis
  • 249
  • 3
  • 17
  • There are some back tracing in the plot. If you want to see them, try adding decimal numbers randomly and plot. – swatchai Nov 30 '20 at 01:58
  • 1
    Does this answer your question? [Matplotlib scatter plot with different text at each data point](https://stackoverflow.com/questions/14432557/matplotlib-scatter-plot-with-different-text-at-each-data-point) You have `df.index`, you have your data points. No difference to the duplicate question. – Mr. T Nov 30 '20 at 08:44

2 Answers2

1

The plot is OK. If you want to check how each vertex is plotted, you need modified data. Here is the modified data (x only) and the plot.

df = pd.DataFrame({
'x_coordinate': [0.1, 0.2, 0.3, 0.4, 1.5, 1.6,-1.7,-1.8,-2.9,0.1],
'y_coordinate': [0, 2, 1, 3,  3, 1,1,-2,2,-1],
})

plotss

Edit

For your new request, the code is modified as follows (full runnable code).

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df = pd.DataFrame({
'x_coordinate': [0.1, 0.2, 0.3, 0.4, 1.5, 1.6,-1.7,-1.8,-2.9,0.1],
'y_coordinate': [0, 2, 1, 3,  3, 1,1,-2,2,-1],
})

fig = plt.figure(figsize=(6,5))
ax1 = fig.add_subplot(1, 1, 1)

df.plot('x_coordinate','y_coordinate', legend=False, ax=ax1)

for ea in zip(np.array((range(len(df)))), df.x_coordinate.values, df.y_coordinate.values):
    text, x, y = "P"+str(ea[0]), ea[1], ea[2]
    ax1.annotate(text, (x,y))

updatefig

swatchai
  • 17,400
  • 3
  • 39
  • 58
  • Sorry.. I didn't quite get it. In this plot also, I see that the starting point is (0.1,0) but after that the line is connected to (-2.9,2) and then to (-1.8,-2), etc. What I want is to start from (0.1,0) and then connect to (0.2,2) and then to (0.3,1) and so on. So, how this plot is okey ? – Johnson Francis Nov 30 '20 at 02:14
  • @JohnsonFrancis In my plot (with my data), the line starts at (x,y)=(0.1, 0) then goes to (0.2, 2), and go thru these coordinates: (0.3, 1), (0.4, 3), (1.5, 3), and so on. – swatchai Nov 30 '20 at 03:03
  • @ swatchai.. Thnx.. Looks like I was overseeing the coordinate and now felt that my original question was totally a stupid one. So, now I edited it slightly for a different ask. Regretting for the inconvenience. – Johnson Francis Nov 30 '20 at 04:51
0

I found an easier way to do it.. Thought to share..

fig, ax = plt.subplots()
df.plot('x_coordinate','y_coordinate',ax=ax)
for k, v in df[['x_coordinate','y_coordinate']].iterrows():
    ax.annotate('p'+str(k+1), v)
plt.show()

enter image description here

Johnson Francis
  • 249
  • 3
  • 17