Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib set_xlim() / set_ylim() or set_xbound() / set_ybound() for zooming to max extent?

This is the structure of my data:

print("test_data: " + str(test_data))

#test_data: [[-1.3869276   0.76189806]
# [-1.3870304   0.76177611]
# [-1.38695643  0.76194005]
# ...,
# [-1.38663699  0.76174211]
# [-1.38670514  0.76161662]
# [-1.3865974   0.76163099]]

It is a numpy array of lat/lng coordinates converted to radians.

I have problems setting the x and y limits/bounds of the matplot-figure. I have tried both set_xlim()/set_ylim() and set_xbound()/set_ybound(), but the results are not satisfactory:

set_xlim() and set_ylim() result in a distorted view of my data: enter image description here

.. while set_xbound() and set_ybound() zooms in too much (and further crops labels of the y-axis): enter image description here

This is my code:

    test_data = np.radians(points)
    #get max data extent
    limXMin = np.max(test_data[0])
    limXMax = np.max(test_data[1])
    limYMax = np.min(test_data[0])
    limYMin = np.min(test_data[1])
    #print max data extent:
    print(limXMin)
    print(limXMax)
    print(limYMin)
    print(limYMax)
    #0.761924200543
    #0.761882818787
    #-1.38701148104
    #-1.3868174877

    #create figure
    fig = plt.figure(figsize=(11, 8), dpi=80)
    #print current extent
    print(str(plt.xlim()))
    print(str(plt.ylim()))
    #(0.0, 1.0)
    #(0.0, 1.0)
    ax = fig.add_subplot(1,1,1) # 1 Row, 1 Column and the first axes in this grid
    plt.scatter(test_data.T[0], test_data.T[1], color='b', **plot_kwds)
    #modify extent (Fig.1)
    ax.set_xlim([limXMin, limXMax])
    ax.set_ylim([limYMin, limYMax])
    #modify extent (Fig.2)
    ax.set_xbound(limXMin, limXMax)
    ax.set_ybound(limYMin, limYMax)
    ax.figure.savefig(pathname + '/graph_1.png')

Instead, if I do this:

    plt.figure(figsize=(11, 8), dpi=80)
    plt.autoscale(enable=True)
    fig = plt.scatter(test_data.T[0], test_data.T[1], color='b', **plot_kwds)
    fig.figure.savefig(pathname + '/graph_1.png')

I get the full extent but with too much border. Almost half of the plot area is not showing data:

enter image description here

like image 649
Alex Avatar asked Oct 21 '25 04:10

Alex


2 Answers

Something went wrong with your definition of min and max values. Additionally you probably also forgot to transpose your data. It should probably be

limXMin = np.min(test_data.T[0])
limXMax = np.max(test_data.T[0])

limYMin = np.min(test_data.T[1])
limYMax = np.max(test_data.T[1])

Using the provided data,

set_xlim()/set_ylim and set_xbound()/set_ybound() both produce the same figure:

like image 99
ImportanceOfBeingErnest Avatar answered Oct 23 '25 19:10

ImportanceOfBeingErnest


After realizing that I need to transpose my numpy array, I managed to get the correct results:

    limYMin = np.min(test_data.T[1])
    limYMax = np.max(test_data.T[1])
    limXMin = np.min(test_data.T[0])
    limXMax = np.max(test_data.T[0])

    fig = plt.figure(figsize=(11, 11), dpi=80)
    plt.gca().set_xlim([limXMin, limXMax])
    plt.gca().set_ylim([limYMin, limYMax])
    fig = plt.scatter(test_data.T[0], test_data.T[1], color='b', **plot_kwds)
    fig.figure.savefig(pathname + 'graph_01.png')

The .T somehow turns the array around, so it is accessible in rows and not columns. Further, by using plt.gca().set_xlim and plt.gca().set_ylim I could avoid creating axes and sub_plots (e.g. ax1 = plt.subplot(131) and then ax.set_xlim(left=limXMin,right=limXMax)).

Many thanks to ImportanceOfBeingErnest for pointing me in the right direction (see answer above)!

enter image description here

like image 37
Alex Avatar answered Oct 23 '25 20:10

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!