Matplotlib. co-ordinates for scatter plot are off

E

eric.garlic

Hi,

I have been following a very good online tutorial for matplotlib:

http://www.loria.fr/~rougier/teaching/matplotlib/#introduction

However, when I try to annotate the point where the cosine/sine graphs cross the scatter graph misses the point where it crosses the axes; green and pink colours.

Anyone know why and/or what I have done wrong?

Help appreciated


_______________________CODE BELOW_________________

#the points
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
C,S=np.cos(x), np.sin(x)

#axis limits
xlim(x.min()*1.1, x.max()*1.1)
ylim(C.min()*1.1,C.max()*1.1)


#spines, there are four (top, bottom, left and right).
#spines, remove top and right, and move left and bottom
#gca returns current axis instance. Used to control axis properties using either set or "Axes" methods
#I assume "gca" stands for Get Current Axis, like "gcf" stands for Get Current Figure
ax=gca()
#spines
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
#ticks
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
#reposition
ax.spines['left'].set_position(('data',0))
ax.spines['bottom'].set_position(('data',0))


#ticks ticks as tuples and labels as LaTex tuples.
#Note: if these next two lines of code go before the spine changes the labelling is lost
xticks([-np.pi,-np.pi/2,0,np.pi/2,np.pi],[r'$-\pi$',r'$-\pi/2$',r'$0$',r'$+\pi/2$',r'$+\pi$'])
yticks([-1,0,1],[r'$-1$',r'0',r'$+1$'])


#plotting
plot(x,C,color="blue", linewidth = 2.5, linestyle="dotted",label = 'Sine')
plot(x,S, color="red", linewidth="2.5",linestyle="-.", label='Cosine')

#legend. Note remember to add label to plot statement e.g. 'Sine' as the legend requires it
#NOTE: legend has to go after plot statement in order to get the graph labels
legend(loc='upper left')

#Annotation
#the green point. NOTE: use the scatter method
t=pi/4
scatter([t,],[np.cos(t),],color='green',linewidth=10)
#draw the lines with plot
plot([t,cos(t)],[t,0],color='green')
plot([0,cos(t)],[t,cos(t)],color='pink')

scatter([t,],[0,],color='black')
scatter([0,],[cos(t),],color='black')

show()
 
E

EricGarlic

Correction: I meant Plot not Scatter - corrected below. Issue is with Plot statement


Hi,
I have been following a very good online tutorial for matplotlib:



http://www.loria.fr/~rougier/teaching/matplotlib/#introduction



However, when I try to annotate the point where the cosine/sine graphs cross the plot graph misses the point where it crosses the axes; green and pink colours.



Anyone know why and/or what I have done wrong?



Help appreciated





_______________________CODE BELOW_________________



#the points

x = np.linspace(-np.pi, np.pi, 256, endpoint=True)

C,S=np.cos(x), np.sin(x)



#axis limits

xlim(x.min()*1.1, x.max()*1.1)

ylim(C.min()*1.1,C.max()*1.1)





#spines, there are four (top, bottom, left and right).

#spines, remove top and right, and move left and bottom

#gca returns current axis instance. Used to control axis properties using either set or "Axes" methods

#I assume "gca" stands for Get Current Axis, like "gcf" stands for Get Current Figure

ax=gca()

#spines

ax.spines['right'].set_color('none')

ax.spines['top'].set_color('none')

#ticks

ax.xaxis.set_ticks_position('bottom')

ax.yaxis.set_ticks_position('left')

#reposition

ax.spines['left'].set_position(('data',0))

ax.spines['bottom'].set_position(('data',0))





#ticks ticks as tuples and labels as LaTex tuples.

#Note: if these next two lines of code go before the spine changes the labelling is lost

xticks([-np.pi,-np.pi/2,0,np.pi/2,np.pi],[r'$-\pi$',r'$-\pi/2$',r'$0$',r'$+\pi/2$',r'$+\pi$'])

yticks([-1,0,1],[r'$-1$',r'0',r'$+1$'])





#plotting

plot(x,C,color="blue", linewidth = 2.5, linestyle="dotted",label = 'Sine')

plot(x,S, color="red", linewidth="2.5",linestyle="-.", label='Cosine')



#legend. Note remember to add label to plot statement e.g. 'Sine' as the legend requires it

#NOTE: legend has to go after plot statement in order to get the graph labels

legend(loc='upper left')



#Annotation

#the green point. NOTE: use the scatter method

t=pi/4

scatter([t,],[np.cos(t),],color='green',linewidth=10)

#draw the lines with plot

plot([t,cos(t)],[t,0],color='green')

plot([0,cos(t)],[t,cos(t)],color='pink')



scatter([t,],[0,],color='black')

scatter([0,],[cos(t),],color='black')



show()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top