python - matplotlib: Have axis maintaining ratio -


i new matplotlib, , have simple (i'm guessing) question.

i have data need represented in rectangle of 50x70 "units" (they're feet, representing room) don't seem able matplotlib drawing rectangle same scale on both axis , keeping 50x70 "dimensions" @ same time.

i've tried following:

import json import matplotlib import os import sys import traceback import matplotlib.pyplot plt  data_file = os.path.join(os.path.expanduser("~"), "results.json") floor_dimensions = (50, 70)  if __name__ == "__main__":     if len(sys.argv) > 1:         data_file = os.path.abspath(sys.argv[0])     print "gonna see happens file %s" % data_file     try:         open(data_file, 'r') f:             result_dict = json.load(f)     except (ioerror, oserror, valueerror), e:         print "received %s %s when trying parse json %s\n"\         "showing traceback: %s" % (type(e), e, data_file, traceback.format_exc())         result_dict = {}         d_mac in result_dict:         data = result_dict[d_mac]         if len(data) < 3:             continue         x_s = list(d['x'] d in data)         y_s  = list(d['y'] d in data)         plt.scatter(x_s, y_s, marker='o', c=numpy.random.rand(5,1), s=15)     plt.xlim([0, floor_dimensions[0]])     plt.ylim([0, floor_dimensions[1]])     #plt.axis('equal')     plt.show()     sys.exit(0) 

doing that, get:

enter image description here

which draws data inside square, changing x-y scale (x 50 points, , y 70, therefor y shows "shrunk")

another option tried uncommenting line saying plt.axis('equal'), "cuts" y axis (doesn't start in 0 , finishes in 70, starts in 15 , ends in 55, because there's no data y < 15 , y > 55)

enter image description here

but don't want either, want "canvas" starting in y=0 , ending in y=70, , if there's no data show empty space.

what need draw this:

enter image description here

which got manually re-sizing window plot rendered :-d

thank in advance!

add plt.axis('scaled').

edit: axis('image') may better needs.

more axis settings can found in the documentation.

import matplotlib.pyplot plt import numpy np  xs = np.arange(50) ys = (np.random.random(50)*70) + 15  plt.scatter(xs,ys)  plt.axis('image') plt.axis([0, 50, 0, 70])  plt.show() 

gives:

correct

in updated example know ys has maximum of ~85, offset demonstrate proper axis enforcement.


Comments