4

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

Alex
  • 2,784
  • 2
  • 32
  • 46
  • 1
    For those who are looking for the difference between bounds and limits, the answer is here: [In matplotlib, what is the difference betweent set_xlim and set_xbound?](https://stackoverflow.com/q/11459672/774575) – mins Jun 12 '23 at 10:05

2 Answers2

2

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:

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • I am aware of this, but then I get limXMin = -1.3868174877 and limXMax = 0.761924200543 / limYMin = -1.38683883307 and limYMax = 0.761675019886 – Alex Dec 15 '17 at 09:17
  • I really hope you are not expecting us to test anything here without you providing a [mcve] of an issue. – ImportanceOfBeingErnest Dec 15 '17 at 09:25
  • Many apologies if I was unclear, I added the structure of my data to the original post – Alex Dec 15 '17 at 09:34
  • Yes, so using your data, I would expect the above to work exactly as desired. See image in edited answer. – ImportanceOfBeingErnest Dec 15 '17 at 09:46
  • Many thanks! The problem really was the missing Transpose of my numpy array. I am still getting confused with pandas.dataframes, numpy.ndarray and other data structures, but looking at the prints of these made me realize my mistake. If you compare the last plot of my first post with the correct one in my answer below, it is also visible that I accidentally mirrored my x and y-axes – Alex Dec 15 '17 at 09:50
1

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

Alex
  • 2,784
  • 2
  • 32
  • 46