pythonmatplotlibplotfiguregraphlab

Change x and y input range in Hexbin plot in Python


I created a hexbin plot using the following code in Python:

df.plot(kind='hexbin', x='NO2', y='pre', gridsize=40, sharex = False)

and I get this figure,

figure 1

I want to change the ranges of input x and y from default to [0,100]. I used the following line:

ylim = [0, 100], xlim  =[0, 100] 

However, with these lines added my figure gets distorted and mapped to a 100 by 100 grid with blank spaces appear on edges as below.

figure 2

How can I change the input x and y range and not just distort the existing figure?


Solution

  • The extent keyword argument sets the range of values to include in the binning. So to have a hexbin plot with a range between 0 and 100 in both directions, you may use extent=[0,100,0,100],

    df.plot(kind='hexbin',x='NO2',y='pre',gridsize=40, linewidths=0, extent=[0,100,0,100])