简体   繁体   中英

Plotting Coordinate Lines Using Matplotlib

I want to plot the co-ordinate lines of a co-ordinate system (eg Cartesian co-ords) using matplotlib.

Then I want to transform them using some linear transform (skew, scale, rotate, etc.), and I want to plot this transformed version of the system as well.

I am quite new to matplotlib and I have no idea as to how I could go about doing this. Any suggestions?

Something like this:

在此处输入图片说明

Doesn't have to be on the same plot as above, I just want to be able to plot the lines (and shapes and their transformed versions as well).

EDIT: If you instead have a MATLAB solution, I'll take that too.

This should get you started on the right track

import matplotlib

import matplotlib.pyplot as plt

xx = range(10)
yy = range(10)
[plt.plot([x,x],[min(yy),max(yy)],color='k') for x in xx]
[plt.plot([min(xx),max(xx)],[y,y],color='k') for y in yy]

user2539738's answer demonstrates how to draw a grid in a plot. The next step is applying a transform. This is a mathematical operation which can be described as a function of the x and y coordinates. For example, a shear transform like your example images -

def my_transform(x, y):
    return (x+y/2, y)

With this in mind, you can plot the transformed grid. You simply have to calculate the new coordinates:

# Transformed grid
for x in xx:
    (x1, y1) = my_transform(x, min(yy))
    (x2, y2) = my_transform(x, max(yy))
    plt.plot([x1,x2],[y1,y2],color='r')
for y in yy:
    (x1, y1) = my_transform(min(xx), y)
    (x2, y2) = my_transform(max(xx), y)
    plt.plot([x1,x2],[y1,y2],color='r')

This plots the transformed grid in red. The first for loop plots what were the vertical lines of the grid (going from point x, min(yy) to x, max(yy) ), and the second plots the horizontal lines. The transform function is applied to the original pairs of points to calculate the new endpoints of the transformed line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM