How I can get cartesian coordinate system in matplotlib?

后端 未结 5 766
悲哀的现实
悲哀的现实 2020-12-03 07:11

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 refe

5条回答
  •  被撕碎了的回忆
    2020-12-03 08:05

    Here is another way to draw a Cartesian coordinate system, built on the answers already given by others.

    import matplotlib.pyplot as plt
    import numpy as np
    
    fig, ax = plt.subplots(figsize=(10, 10))
    
    # Enter x,y coordinates and colors of points
    x = [0, 2, -3, -1.5]
    y = [0, 3, 1, -2.5]
    color = ['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
    ax.scatter(x, y, c=color)
    
    # Draw lines connecting points to axes
    [ax.plot([dot_x, dot_x], [0, dot_y], c=c, ls='--', lw=1.5, alpha=0.5) for dot_x, dot_y, c in zip(x, y, color)] 
    [ax.plot([0, dot_x], [dot_y, dot_y], c=c, ls='--', lw=1.5, alpha=0.5) for dot_x, dot_y, c in zip(x, y, color)]
    
    # 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 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)
    ax.set_xticks(np.arange(xmin,xmax+1), minor=True)
    ax.set_yticks(np.arange(ymin,ymax+1), minor=True)
    
    # Draw grid lines
    ax.grid(which='major', color='grey', linewidth=1, linestyle='-', alpha=0.2)
    ax.grid(which='minor', 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()
    

    Cartesian coordinate system

    Notice that I have not added annotations displaying the coordinates of the points as in my experience this requires a lot more code to place them nicely and with minimal overlapping. To get annotations, it is probably easier to use an interactive graphing library such as Plotly.

提交回复
热议问题