如何在matplotlib中获得笛卡尔坐标系? [英] How I can get cartesian coordinate system in matplotlib?
问题描述
我是使用Python进行绘图的新手,无法真正找到问题的答案:如何在matplotlib中获得笛卡尔坐标平面?我的意思是垂直参考线(坐标轴)以箭头结尾,在原点(0,0)处相交,原点在图的中心.
I am new to plotting with Python and can't really find an answer to the question: How can I get Cartesian coordinate plane in matplotlib? By this I mean perpendicular reference lines (coordinate axis) ended up with arrows, intersecting at the origin, (0,0), with the origin at the center of the plot.
考虑一架用于做高中几何学的飞机,以下是我需要达到的目标的完美示例:
Think about a a plane for doing high school geomtery, the following is a perfect example of what I need to achieve:
推荐答案
如果只想绘制一些点,则需要散点图
If you just want to plot some dots, scatter is what you want
from pylab import *
x = [0,2,-3,-1.5]
y = [0,3,1,-2.5]
color=['m','g','r','b']
scatter(x,y, s=100 ,marker='o', c=color)
show()
用于漂亮的打印(带有箭头和虚线):
For pretty printing ( with arrows and dashed lines ) :
from pylab import *
import matplotlib.pyplot as plt
x = [0,2,-3,-1.5]
y = [0,3,1,-2.5]
color=['m','g','r','b']
fig = plt.figure()
ax = fig.add_subplot(111)
scatter(x,y, s=100 ,marker='o', c=color)
[ plot( [dot_x,dot_x] ,[0,dot_y], '-', linewidth = 3 ) for dot_x,dot_y in zip(x,y) ]
[ plot( [0,dot_x] ,[dot_y,dot_y], '-', linewidth = 3 ) for dot_x,dot_y in zip(x,y) ]
left,right = ax.get_xlim()
low,high = ax.get_ylim()
arrow( left, 0, right -left, 0, length_includes_head = True, head_width = 0.15 )
arrow( 0, low, 0, high-low, length_includes_head = True, head_width = 0.15 )
grid()
show()
仍有一些工作要做,但与结果相距不远:
There is still some work to do, but it is not far from the result :
这篇关于如何在matplotlib中获得笛卡尔坐标系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!