Line plot for each coordinate in matplot lib
up vote
0
down vote
favorite
I am trying to plot the lines that connect starting (x,y) and ending (x,y)
That means a line will be connecting (x1start,y1start) to (x1end,y1end)
I have multiple rows in data frame.
The sample data frame that replicate the actual dataframe and shown below:
df = pd.DataFrame()
df ['Xstart'] = [1,2,3,4,5]
df ['Xend'] = [6,8,9,10,12]
df ['Ystart'] = [0,1,2,3,4]
df ['Yend'] = [6,8,9,10,12]
According to that, if we look at the first row of df, a line will be connecting (1,0) to (6,6)
For that I am using for loop to draw a line for each row as follow:
fig,ax = plt.subplots()
fig.set_size_inches(7,5)
for i in range (len(df)):
ax.plot((df.iloc[i]['Xstart'],df.iloc[i]['Xend']),(df.iloc[i]['Ystart'],df.iloc[i]['Yend']))
ax.annotate("",xy = (df.iloc[i]['Xstart'],df.iloc[i]['Xend']),
xycoords = 'data',
xytext = (df.iloc[i]['Ystart'],df.iloc[i]['Yend']),
textcoords = 'data',
arrowprops = dict(arrowstyle = "->", connectionstyle = 'arc3', color = 'blue'))
plt.show()
I have the following error message when I run this.
I got the figure as shown below:
The arrow and line are in as expected. the arrow should be on the end point of each line.
Can anyone advise what is going on here?
Thanks,
Zep
python pandas matplotlib annotate
add a comment |
up vote
0
down vote
favorite
I am trying to plot the lines that connect starting (x,y) and ending (x,y)
That means a line will be connecting (x1start,y1start) to (x1end,y1end)
I have multiple rows in data frame.
The sample data frame that replicate the actual dataframe and shown below:
df = pd.DataFrame()
df ['Xstart'] = [1,2,3,4,5]
df ['Xend'] = [6,8,9,10,12]
df ['Ystart'] = [0,1,2,3,4]
df ['Yend'] = [6,8,9,10,12]
According to that, if we look at the first row of df, a line will be connecting (1,0) to (6,6)
For that I am using for loop to draw a line for each row as follow:
fig,ax = plt.subplots()
fig.set_size_inches(7,5)
for i in range (len(df)):
ax.plot((df.iloc[i]['Xstart'],df.iloc[i]['Xend']),(df.iloc[i]['Ystart'],df.iloc[i]['Yend']))
ax.annotate("",xy = (df.iloc[i]['Xstart'],df.iloc[i]['Xend']),
xycoords = 'data',
xytext = (df.iloc[i]['Ystart'],df.iloc[i]['Yend']),
textcoords = 'data',
arrowprops = dict(arrowstyle = "->", connectionstyle = 'arc3', color = 'blue'))
plt.show()
I have the following error message when I run this.
I got the figure as shown below:
The arrow and line are in as expected. the arrow should be on the end point of each line.
Can anyone advise what is going on here?
Thanks,
Zep
python pandas matplotlib annotate
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to plot the lines that connect starting (x,y) and ending (x,y)
That means a line will be connecting (x1start,y1start) to (x1end,y1end)
I have multiple rows in data frame.
The sample data frame that replicate the actual dataframe and shown below:
df = pd.DataFrame()
df ['Xstart'] = [1,2,3,4,5]
df ['Xend'] = [6,8,9,10,12]
df ['Ystart'] = [0,1,2,3,4]
df ['Yend'] = [6,8,9,10,12]
According to that, if we look at the first row of df, a line will be connecting (1,0) to (6,6)
For that I am using for loop to draw a line for each row as follow:
fig,ax = plt.subplots()
fig.set_size_inches(7,5)
for i in range (len(df)):
ax.plot((df.iloc[i]['Xstart'],df.iloc[i]['Xend']),(df.iloc[i]['Ystart'],df.iloc[i]['Yend']))
ax.annotate("",xy = (df.iloc[i]['Xstart'],df.iloc[i]['Xend']),
xycoords = 'data',
xytext = (df.iloc[i]['Ystart'],df.iloc[i]['Yend']),
textcoords = 'data',
arrowprops = dict(arrowstyle = "->", connectionstyle = 'arc3', color = 'blue'))
plt.show()
I have the following error message when I run this.
I got the figure as shown below:
The arrow and line are in as expected. the arrow should be on the end point of each line.
Can anyone advise what is going on here?
Thanks,
Zep
python pandas matplotlib annotate
I am trying to plot the lines that connect starting (x,y) and ending (x,y)
That means a line will be connecting (x1start,y1start) to (x1end,y1end)
I have multiple rows in data frame.
The sample data frame that replicate the actual dataframe and shown below:
df = pd.DataFrame()
df ['Xstart'] = [1,2,3,4,5]
df ['Xend'] = [6,8,9,10,12]
df ['Ystart'] = [0,1,2,3,4]
df ['Yend'] = [6,8,9,10,12]
According to that, if we look at the first row of df, a line will be connecting (1,0) to (6,6)
For that I am using for loop to draw a line for each row as follow:
fig,ax = plt.subplots()
fig.set_size_inches(7,5)
for i in range (len(df)):
ax.plot((df.iloc[i]['Xstart'],df.iloc[i]['Xend']),(df.iloc[i]['Ystart'],df.iloc[i]['Yend']))
ax.annotate("",xy = (df.iloc[i]['Xstart'],df.iloc[i]['Xend']),
xycoords = 'data',
xytext = (df.iloc[i]['Ystart'],df.iloc[i]['Yend']),
textcoords = 'data',
arrowprops = dict(arrowstyle = "->", connectionstyle = 'arc3', color = 'blue'))
plt.show()
I have the following error message when I run this.
I got the figure as shown below:
The arrow and line are in as expected. the arrow should be on the end point of each line.
Can anyone advise what is going on here?
Thanks,
Zep
python pandas matplotlib annotate
python pandas matplotlib annotate
edited Nov 18 at 15:25
asked Nov 18 at 14:54
Zephyr
4029
4029
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
You're mixing up the positions of the arrows. Each coordinate pair in xy
and xytext
consists of an x and y value.
Also in order to see the arrows in the plot you need to set the limits of the plot manually, because annotations are - for good reason - not taken into account when scaling the data limits.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame()
df ['Xstart'] = [1,2,3,4,5]
df ['Xend'] = [6,8,9,10,12]
df ['Ystart'] = [0,1,2,3,4]
df ['Yend'] = [6,8,9,10,12]
fig,ax = plt.subplots()
fig.set_size_inches(7,5)
for i in range (len(df)):
ax.annotate("",xy = (df.iloc[i]['Xend'],df.iloc[i]['Yend']),
xycoords = 'data',
xytext = (df.iloc[i]['Xstart'],df.iloc[i]['Ystart']),
textcoords = 'data',
arrowprops = dict(arrowstyle = "->",
connectionstyle = 'arc3', color = 'blue'))
ax.set(xlim=(df[["Xstart","Xend"]].values.min(), df[["Xstart","Xend"]].values.max()),
ylim=(df[["Ystart","Yend"]].values.min(), df[["Ystart","Yend"]].values.max()))
plt.show()
Thanks for the reply. I solved the turple, But when I plot it i just got blank figure. I have updated in the question.
– Zephyr
Nov 18 at 15:18
Updated the answer
– ImportanceOfBeingErnest
Nov 18 at 15:31
Thanks. Appreciate much for your help
– Zephyr
Nov 18 at 15:38
add a comment |
up vote
1
down vote
If you want to plot the line segments, the following code works. You may want arrows or some sort of annotate
element (notice correct spelling), but your goal seems to be plotting the line segments, which this accomplishes:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame()
df ['Xstart'] = [1,2,3,4,5]
df ['Xend'] = [6,8,9,10,12]
df ['Ystart'] = [0,1,2,3,4]
df ['Yend'] = [6,8,9,10,12]
fig = plt.figure()
ax = fig.add_subplot(111)
for i in range (len(df)):
ax.plot(
(df.iloc[i]['Xstart'],df.iloc[i]['Xend']),
(df.iloc[i]['Ystart'],df.iloc[i]['Yend'])
)
plt.show()
I tried to add arrow and sth wrong. update in the question.
– Zephyr
Nov 18 at 15:23
add a comment |
up vote
0
down vote
Not 100% certain but I think in line two you need to make the part after xy= a tuple because otherwise it sets the part in front of the , as keyword parameter and tries passing the part after the , as normal arg
Thanks for the reply. I solved the turple, But when I plot it i just got blank figure. I have updated in the question.
– Zephyr
Nov 18 at 15:19
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You're mixing up the positions of the arrows. Each coordinate pair in xy
and xytext
consists of an x and y value.
Also in order to see the arrows in the plot you need to set the limits of the plot manually, because annotations are - for good reason - not taken into account when scaling the data limits.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame()
df ['Xstart'] = [1,2,3,4,5]
df ['Xend'] = [6,8,9,10,12]
df ['Ystart'] = [0,1,2,3,4]
df ['Yend'] = [6,8,9,10,12]
fig,ax = plt.subplots()
fig.set_size_inches(7,5)
for i in range (len(df)):
ax.annotate("",xy = (df.iloc[i]['Xend'],df.iloc[i]['Yend']),
xycoords = 'data',
xytext = (df.iloc[i]['Xstart'],df.iloc[i]['Ystart']),
textcoords = 'data',
arrowprops = dict(arrowstyle = "->",
connectionstyle = 'arc3', color = 'blue'))
ax.set(xlim=(df[["Xstart","Xend"]].values.min(), df[["Xstart","Xend"]].values.max()),
ylim=(df[["Ystart","Yend"]].values.min(), df[["Ystart","Yend"]].values.max()))
plt.show()
Thanks for the reply. I solved the turple, But when I plot it i just got blank figure. I have updated in the question.
– Zephyr
Nov 18 at 15:18
Updated the answer
– ImportanceOfBeingErnest
Nov 18 at 15:31
Thanks. Appreciate much for your help
– Zephyr
Nov 18 at 15:38
add a comment |
up vote
1
down vote
accepted
You're mixing up the positions of the arrows. Each coordinate pair in xy
and xytext
consists of an x and y value.
Also in order to see the arrows in the plot you need to set the limits of the plot manually, because annotations are - for good reason - not taken into account when scaling the data limits.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame()
df ['Xstart'] = [1,2,3,4,5]
df ['Xend'] = [6,8,9,10,12]
df ['Ystart'] = [0,1,2,3,4]
df ['Yend'] = [6,8,9,10,12]
fig,ax = plt.subplots()
fig.set_size_inches(7,5)
for i in range (len(df)):
ax.annotate("",xy = (df.iloc[i]['Xend'],df.iloc[i]['Yend']),
xycoords = 'data',
xytext = (df.iloc[i]['Xstart'],df.iloc[i]['Ystart']),
textcoords = 'data',
arrowprops = dict(arrowstyle = "->",
connectionstyle = 'arc3', color = 'blue'))
ax.set(xlim=(df[["Xstart","Xend"]].values.min(), df[["Xstart","Xend"]].values.max()),
ylim=(df[["Ystart","Yend"]].values.min(), df[["Ystart","Yend"]].values.max()))
plt.show()
Thanks for the reply. I solved the turple, But when I plot it i just got blank figure. I have updated in the question.
– Zephyr
Nov 18 at 15:18
Updated the answer
– ImportanceOfBeingErnest
Nov 18 at 15:31
Thanks. Appreciate much for your help
– Zephyr
Nov 18 at 15:38
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You're mixing up the positions of the arrows. Each coordinate pair in xy
and xytext
consists of an x and y value.
Also in order to see the arrows in the plot you need to set the limits of the plot manually, because annotations are - for good reason - not taken into account when scaling the data limits.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame()
df ['Xstart'] = [1,2,3,4,5]
df ['Xend'] = [6,8,9,10,12]
df ['Ystart'] = [0,1,2,3,4]
df ['Yend'] = [6,8,9,10,12]
fig,ax = plt.subplots()
fig.set_size_inches(7,5)
for i in range (len(df)):
ax.annotate("",xy = (df.iloc[i]['Xend'],df.iloc[i]['Yend']),
xycoords = 'data',
xytext = (df.iloc[i]['Xstart'],df.iloc[i]['Ystart']),
textcoords = 'data',
arrowprops = dict(arrowstyle = "->",
connectionstyle = 'arc3', color = 'blue'))
ax.set(xlim=(df[["Xstart","Xend"]].values.min(), df[["Xstart","Xend"]].values.max()),
ylim=(df[["Ystart","Yend"]].values.min(), df[["Ystart","Yend"]].values.max()))
plt.show()
You're mixing up the positions of the arrows. Each coordinate pair in xy
and xytext
consists of an x and y value.
Also in order to see the arrows in the plot you need to set the limits of the plot manually, because annotations are - for good reason - not taken into account when scaling the data limits.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame()
df ['Xstart'] = [1,2,3,4,5]
df ['Xend'] = [6,8,9,10,12]
df ['Ystart'] = [0,1,2,3,4]
df ['Yend'] = [6,8,9,10,12]
fig,ax = plt.subplots()
fig.set_size_inches(7,5)
for i in range (len(df)):
ax.annotate("",xy = (df.iloc[i]['Xend'],df.iloc[i]['Yend']),
xycoords = 'data',
xytext = (df.iloc[i]['Xstart'],df.iloc[i]['Ystart']),
textcoords = 'data',
arrowprops = dict(arrowstyle = "->",
connectionstyle = 'arc3', color = 'blue'))
ax.set(xlim=(df[["Xstart","Xend"]].values.min(), df[["Xstart","Xend"]].values.max()),
ylim=(df[["Ystart","Yend"]].values.min(), df[["Ystart","Yend"]].values.max()))
plt.show()
edited Nov 18 at 15:30
answered Nov 18 at 15:00
ImportanceOfBeingErnest
119k10120192
119k10120192
Thanks for the reply. I solved the turple, But when I plot it i just got blank figure. I have updated in the question.
– Zephyr
Nov 18 at 15:18
Updated the answer
– ImportanceOfBeingErnest
Nov 18 at 15:31
Thanks. Appreciate much for your help
– Zephyr
Nov 18 at 15:38
add a comment |
Thanks for the reply. I solved the turple, But when I plot it i just got blank figure. I have updated in the question.
– Zephyr
Nov 18 at 15:18
Updated the answer
– ImportanceOfBeingErnest
Nov 18 at 15:31
Thanks. Appreciate much for your help
– Zephyr
Nov 18 at 15:38
Thanks for the reply. I solved the turple, But when I plot it i just got blank figure. I have updated in the question.
– Zephyr
Nov 18 at 15:18
Thanks for the reply. I solved the turple, But when I plot it i just got blank figure. I have updated in the question.
– Zephyr
Nov 18 at 15:18
Updated the answer
– ImportanceOfBeingErnest
Nov 18 at 15:31
Updated the answer
– ImportanceOfBeingErnest
Nov 18 at 15:31
Thanks. Appreciate much for your help
– Zephyr
Nov 18 at 15:38
Thanks. Appreciate much for your help
– Zephyr
Nov 18 at 15:38
add a comment |
up vote
1
down vote
If you want to plot the line segments, the following code works. You may want arrows or some sort of annotate
element (notice correct spelling), but your goal seems to be plotting the line segments, which this accomplishes:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame()
df ['Xstart'] = [1,2,3,4,5]
df ['Xend'] = [6,8,9,10,12]
df ['Ystart'] = [0,1,2,3,4]
df ['Yend'] = [6,8,9,10,12]
fig = plt.figure()
ax = fig.add_subplot(111)
for i in range (len(df)):
ax.plot(
(df.iloc[i]['Xstart'],df.iloc[i]['Xend']),
(df.iloc[i]['Ystart'],df.iloc[i]['Yend'])
)
plt.show()
I tried to add arrow and sth wrong. update in the question.
– Zephyr
Nov 18 at 15:23
add a comment |
up vote
1
down vote
If you want to plot the line segments, the following code works. You may want arrows or some sort of annotate
element (notice correct spelling), but your goal seems to be plotting the line segments, which this accomplishes:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame()
df ['Xstart'] = [1,2,3,4,5]
df ['Xend'] = [6,8,9,10,12]
df ['Ystart'] = [0,1,2,3,4]
df ['Yend'] = [6,8,9,10,12]
fig = plt.figure()
ax = fig.add_subplot(111)
for i in range (len(df)):
ax.plot(
(df.iloc[i]['Xstart'],df.iloc[i]['Xend']),
(df.iloc[i]['Ystart'],df.iloc[i]['Yend'])
)
plt.show()
I tried to add arrow and sth wrong. update in the question.
– Zephyr
Nov 18 at 15:23
add a comment |
up vote
1
down vote
up vote
1
down vote
If you want to plot the line segments, the following code works. You may want arrows or some sort of annotate
element (notice correct spelling), but your goal seems to be plotting the line segments, which this accomplishes:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame()
df ['Xstart'] = [1,2,3,4,5]
df ['Xend'] = [6,8,9,10,12]
df ['Ystart'] = [0,1,2,3,4]
df ['Yend'] = [6,8,9,10,12]
fig = plt.figure()
ax = fig.add_subplot(111)
for i in range (len(df)):
ax.plot(
(df.iloc[i]['Xstart'],df.iloc[i]['Xend']),
(df.iloc[i]['Ystart'],df.iloc[i]['Yend'])
)
plt.show()
If you want to plot the line segments, the following code works. You may want arrows or some sort of annotate
element (notice correct spelling), but your goal seems to be plotting the line segments, which this accomplishes:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame()
df ['Xstart'] = [1,2,3,4,5]
df ['Xend'] = [6,8,9,10,12]
df ['Ystart'] = [0,1,2,3,4]
df ['Yend'] = [6,8,9,10,12]
fig = plt.figure()
ax = fig.add_subplot(111)
for i in range (len(df)):
ax.plot(
(df.iloc[i]['Xstart'],df.iloc[i]['Xend']),
(df.iloc[i]['Ystart'],df.iloc[i]['Yend'])
)
plt.show()
edited Nov 18 at 15:34
answered Nov 18 at 15:19
PJW
591622
591622
I tried to add arrow and sth wrong. update in the question.
– Zephyr
Nov 18 at 15:23
add a comment |
I tried to add arrow and sth wrong. update in the question.
– Zephyr
Nov 18 at 15:23
I tried to add arrow and sth wrong. update in the question.
– Zephyr
Nov 18 at 15:23
I tried to add arrow and sth wrong. update in the question.
– Zephyr
Nov 18 at 15:23
add a comment |
up vote
0
down vote
Not 100% certain but I think in line two you need to make the part after xy= a tuple because otherwise it sets the part in front of the , as keyword parameter and tries passing the part after the , as normal arg
Thanks for the reply. I solved the turple, But when I plot it i just got blank figure. I have updated in the question.
– Zephyr
Nov 18 at 15:19
add a comment |
up vote
0
down vote
Not 100% certain but I think in line two you need to make the part after xy= a tuple because otherwise it sets the part in front of the , as keyword parameter and tries passing the part after the , as normal arg
Thanks for the reply. I solved the turple, But when I plot it i just got blank figure. I have updated in the question.
– Zephyr
Nov 18 at 15:19
add a comment |
up vote
0
down vote
up vote
0
down vote
Not 100% certain but I think in line two you need to make the part after xy= a tuple because otherwise it sets the part in front of the , as keyword parameter and tries passing the part after the , as normal arg
Not 100% certain but I think in line two you need to make the part after xy= a tuple because otherwise it sets the part in front of the , as keyword parameter and tries passing the part after the , as normal arg
answered Nov 18 at 15:00
SV-97
879
879
Thanks for the reply. I solved the turple, But when I plot it i just got blank figure. I have updated in the question.
– Zephyr
Nov 18 at 15:19
add a comment |
Thanks for the reply. I solved the turple, But when I plot it i just got blank figure. I have updated in the question.
– Zephyr
Nov 18 at 15:19
Thanks for the reply. I solved the turple, But when I plot it i just got blank figure. I have updated in the question.
– Zephyr
Nov 18 at 15:19
Thanks for the reply. I solved the turple, But when I plot it i just got blank figure. I have updated in the question.
– Zephyr
Nov 18 at 15:19
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53362201%2fline-plot-for-each-coordinate-in-matplot-lib%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown