0

I need to plot a 2D scatter from matplotlib in python 3.2.

But, the max and min values of data are verious greatly.

I need to adjust the grid and tickers so that each grid cell is a square and the number of grid lines connected to tickers should depend on the size of the figure.

UPDATE

I prefer n X n grid lines on X and Y axis respectively. The value of n depends on the max value of X and Y. I want to keep n within 3 to 8. It means that there are not more than 8 grid lines and not less than 3 lines on X and Y direction.


My python ocde:

    #yList is a list of float numbers
    #xList is a list of float numbers

    rcParams['figure.figsize'] = 6, 6
    plt.scatter(xList, yList, s=3, c='g', alpha=0.5)
    plt.Figure(figsize=(1,1), facecolor='w')
    plt.ylim(0, max(yList))
    plt.xlim(0, max(xList))

    plt.grid()
    plt.show()

Currently, each grid cell is rectagular. I need square for each cell.

bastelflp
  • 9,362
  • 7
  • 32
  • 67
user3601704
  • 753
  • 1
  • 14
  • 46
  • possible duplicate of [How to equalize the scales of x-axis and y-axis in Python matplotlib?](http://stackoverflow.com/questions/17990845/how-to-equalize-the-scales-of-x-axis-and-y-axis-in-python-matplotlib) – Foggzie Jul 24 '15 at 22:36

1 Answers1

0

This can be done using ax.set_x/yticks to adjust the grid, then ax.set_aspect to adjust the scale. If you do it correctly (first you correct the ratio due to difference between xmax and ymax, then you select the scale on the number of case), you will get square grid cells

import random
import matplotlib.pyplot as plt
xList = np.random.uniform(0,1,10)
yList = np.random.uniform(0,10,10)

fig = plt.figure()
ax = fig.gca()
plt.rcParams['figure.figsize'] = 6, 6
ax.scatter(xList, yList, s=3, c='g', alpha=0.5)
ax.set_ylim(0, max(yList))
ax.set_xlim(0, max(xList))

#Added code
n_x, ny = 3, 8
ax.set_xticks( np.linspace(*ax.get_xlim(), num=n_x+1) ) #Need 4 points to make 3 intervals
ax.set_yticks( np.linspace(*ax.get_ylim(), num=n_y+1) ) #Need 9 points to make 8 intervals

ax.set_aspect( ax.get_xlim()[1]/ax.get_ylim()[1] * n_y/n_x  ) 
# ax.get_xlim()[1]/ax.get_ylim()[1] correct difference between xmax and ymax, 
# n_y/n_x put the correct scale to get square grid cells

ax.grid()
fig.show()

enter image description here

Yann
  • 534
  • 4
  • 9