我不熟悉使用Python进行绘图,并且无法真正找到问题的答案:如何在matplotlib中获得笛卡尔坐标平面?我的意思是垂直参考线(坐标轴)以箭头结束,在原点(0,0)处相交,原点位于图的中心。
想想一个进行高中地理的飞机,以下是我需要实现的完美范例:
答案 0 :(得分:8)
如果你只想绘制一些点,那么散射就是你想要的
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()
对于漂亮的打印(带箭头和虚线):
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()
还有一些工作要做,但距离结果并不远:
答案 1 :(得分:6)
我认为matplotlib库中的这个例子应该让你足够接近: http://matplotlib.org/examples/axes_grid/demo_axisline_style.html
答案 2 :(得分:4)
这是另一种基于其他人已经给出的答案绘制笛卡尔坐标系的方法。
import matplotlib.pyplot as plt # v 1.19.2
import numpy as np # v 3.3.2
# Enter x and y coordinates of points and colors
xs = [0, 2, -3, -1.5]
ys = [0, 3, 1, -2.5]
colors = ['m', 'g', 'r', 'b']
# Select length of axes and the space between tick labels
xmin, xmax, ymin, ymax = -5, 5, -5, 5
ticks_frequency = 1
# Plot points
fig, ax = plt.subplots(figsize=(10, 10))
ax.scatter(x, y, c=color)
# Draw lines connecting points to axes
[ax.plot([x, x], [0, y], c=c, ls='--', lw=1.5, alpha=0.5) for x, y, c in zip(xs, ys, colors)]
[ax.plot([0, x], [y, y], c=c, ls='--', lw=1.5, alpha=0.5) for x, y, c in zip(xs, ys, colors)]
# Set identical scales for both axes
ax.set(xlim=(xmin-1, xmax+1), ylim=(ymin-1, ymax+1), aspect='equal')
# Set bottom and left spines as x and y axes of coordinate system
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')
# Remove top and right spines
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
# Create 'x' and 'y' labels placed at the end of the axes
ax.set_xlabel('x', size=14, labelpad=15)
ax.set_ylabel('y', size=14, labelpad=15, rotation=0)
ax.xaxis.set_label_coords(1.03, 0.512)
ax.yaxis.set_label_coords(0.5, 1.02)
# Create custom major ticks to determine position of tick labels
x_ticks = np.arange(xmin, xmax+1, ticks_frequency)
x_ticks_major = x_ticks[x_ticks != 0]
y_ticks = np.arange(ymin, ymax+1, ticks_frequency)
y_ticks_major = y_ticks[y_ticks != 0]
ax.set_xticks(x_ticks_major)
ax.set_yticks(y_ticks_major)
# Create custom minor ticks to enable drawing of minor grid lines
ax.set_xticks(np.arange(xmin,xmax+1), minor=True)
ax.set_yticks(np.arange(ymin,ymax+1), minor=True)
# Draw major and minor grid lines
ax.grid(which='both', color='grey', linewidth=1, linestyle='-', alpha=0.2)
# Draw arrows
ax.plot((1), (0), linestyle="", marker=">", markersize=4, color="k",
transform=ax.get_yaxis_transform(), clip_on=False)
ax.plot((0), (1), linestyle="", marker="^", markersize=4, color="k",
transform=ax.get_xaxis_transform(), clip_on=False)
plt.show()
请注意,我没有添加注释来显示点的坐标,因为根据我的经验,这需要大量代码才能将它们很好地放置并且重叠最少。为了获得注释,使用交互式绘图库(例如Plotly)可能会更容易。
答案 3 :(得分:3)
这是一个古老的问题,但是我认为在当今的matplotlib版本中,关键字为spines。您会这样做:
ax = plt.gca()
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
该链接提供了更多示例。
答案 4 :(得分:0)
下面的代码将为您提供笛卡尔平面。
import matplotlib.pyplot as plt
def build_cartesian_plane(max_quadrant_range):
""" The quadrant range controls the range of the quadrants"""
l = []
zeros = []
plt.grid(True, color='b', zorder=0,)
ax = plt.axes()
head_width = float(0.05) * max_quadrant_range
head_length = float(0.1) * max_quadrant_range
ax.arrow(0, 0, max_quadrant_range, 0, head_width=head_width, head_length=head_length, fc='k', ec='k',zorder=100)
ax.arrow(0, 0, -max_quadrant_range, 0, head_width=head_width, head_length=head_length, fc='k', ec='k', zorder=100)
ax.arrow(0, 0, 0, max_quadrant_range, head_width=head_width, head_length=head_length, fc='k', ec='k', zorder=100)
ax.arrow(0, 0, 0, -max_quadrant_range, head_width=head_width, head_length=head_length, fc='k', ec='k', zorder=100)
counter_dash_width = max_quadrant_range * 0.02
dividers = [0,.1,.2,.3,.4, .5, .6, .7, .8, .9, 1]
for i in dividers:
plt.plot([-counter_dash_width, counter_dash_width], [i*max_quadrant_range, i*max_quadrant_range], color='k')
plt.plot([i * max_quadrant_range, i*max_quadrant_range], [-counter_dash_width, counter_dash_width], color='k')
plt.plot([-counter_dash_width, counter_dash_width], [-i * max_quadrant_range, -i * max_quadrant_range], color='k')
plt.plot([-i * max_quadrant_range, -i * max_quadrant_range], [-counter_dash_width, counter_dash_width], color='k')
l.append(i * max_quadrant_range)
l.append(-i * max_quadrant_range)
zeros.append(0)
zeros.append(0)
build_cartesian_plane(10)
plt.show()