I’m trying to use AxesGrid but I’m running into a problem:
I can plot a single pcolor plot:
But when I try to use AxesGrid, my pcolor plot is ending up where I expect my colorbar to be.
I want to have up to 6 of these plots stacked vertically, sharing a common time axis and y (depth) scale.
I’ll try to simplify my code to show what I’m doing:
I have arrays x_grid and y_grid for time and water depth.
z_dim is a dictionary of arrays (one for each plot)
In the plot above it has two arrays.
from matplotlib import pyplot
nrows = len(z_dim) # Number of rows is the number of arrays
My_figure = pyplot.figure(1,(8,8))
my_grid = AxesGrid(My_figure, 111, #Is this always 111?
nrows_ncols = (nrows,1), # Always one column
axes_pad = 0.1,
add_all=True,
share_all=True, # They all share the same time and depth scales
label_mode = “L”,
cbar_location=“right”,
cbar_mode=“each”,
cbar_size=“7%”,
cbar_pad=“2%”,
)
for row_no,parameter in enumerate(z_dim):
ax = my_grid[row_no]
ax = pyplot.pcolor(x_grid,y_grid,z_dim[parameter])
pyplot.draw()
pyplot.show()
I eventually want to end up with something like this matlab output (which I didn’t generate):
but without the duplication of x scales.
I’m new to pyplot and even after reading the documentation much of this is baffling.
This happens because, when the AxesGrid is created, gca() is set to the last axes, which is the last colobar axes.
If you use axes_grid toolkit, you’d better not use pyplot command that works on axes. Instead, use axes method directly.
For example, instead of “pyplot.pcolor(…)” , use “ax.pcolor(…)”.
Regards,
-JJ
···
On Wed, Dec 2, 2009 at 2:18 PM, Ryan Neve <ryan.neve@…287…> wrote:
Hello,
I’m trying to use AxesGrid but I’m running into a problem:
I can plot a single pcolor plot:
But when I try to use AxesGrid, my pcolor plot is ending up where I expect my colorbar to be.
I want to have up to 6 of these plots stacked vertically, sharing a common time axis and y (depth) scale.
I’ll try to simplify my code to show what I’m doing:
I have arrays x_grid and y_grid for time and water depth.
z_dim is a dictionary of arrays (one for each plot)
In the plot above it has two arrays.
from matplotlib import pyplot
nrows = len(z_dim) # Number of rows is the number of arrays
My_figure = pyplot.figure(1,(8,8))
my_grid = AxesGrid(My_figure, 111, #Is this always 111?
nrows_ncols = (nrows,1), # Always one column
axes_pad = 0.1,
add_all=True,
share_all=True, # They all share the same time and depth scales
label_mode = "L",
cbar_location="right",
cbar_mode="each",
cbar_size="7%",
cbar_pad="2%",
)
for row_no,parameter in enumerate(z_dim):
ax = my_grid[row_no]
Thank You,
I think I have a better understanding. In my figure, there are six axes, three for the plots: grid[i] and three for their colorbars: grid.cbar_axes[i].
I changed my code as you suggested and got something like:
I tried all sorts of things, but finally, by setting aspect=False I got it to work. In the documentation, the table says this defaults to True and the explanation of aspect below says it defaults to False. Although I don’t entirely understand what is going on, I think this threw me off.
So then I had this:
… which looks much better, except that there are two sets of x and y axis
labels? This seems to have something to do with the colorbar. I’ve got:
Now I’m trying to get scales and labels on my colorbars.
I tried:
for i,parameter in enumerate(z_dim):
ax = my_grid[i].pcolor(x_grid,y_grid,z_dim[parameter]) # This is the pcolor plot
my_grid[i].set_ylabel('Depth') # Correctly puts a y label on every plot.
cb = my_grid.cbar_axes[i].colorbar(ax) # Puts in a colorbar for this axes?s
cb.set_ylabel(parameter) #It would be nice if this was on the far right next to the colorbar. I don't see it anywhere. Perhaps underneath something?
It looks like perhaps the colorbar axes is inside the ax axes rather than besides it?
In the demo_grid_with_each_cbar example, how would you put a scale and label on the colorbar like in this plot:?
I can put a y_label on each contour plot, but since they all have depth, I’d like to label this only once.
Is there a way to label the entire AxesGrid (or is that subplot?)?
Thank you very much for your help,
-Ryan
···
On Wed, Dec 2, 2009 at 10:21 PM, Jae-Joon Lee <lee.j.joon@…287…> wrote:
This happens because, when the AxesGrid is created, gca() is set to the last axes, which is the last colobar axes.
If you use axes_grid toolkit, you’d better not use pyplot command that works on axes. Instead, use axes method directly.
For example, instead of “pyplot.pcolor(…)” , use “ax.pcolor(…)”.
Regards,
-JJ
On Wed, Dec 2, 2009 at 2:18 PM, Ryan Neve <ryan.neve@…287…> wrote:
Hello,
I’m trying to use AxesGrid but I’m running into a problem:
I can plot a single pcolor plot:
But when I try to use AxesGrid, my pcolor plot is ending up where I expect my colorbar to be.
I want to have up to 6 of these plots stacked vertically, sharing a common time axis and y (depth) scale.
I’ll try to simplify my code to show what I’m doing:
I have arrays x_grid and y_grid for time and water depth.
z_dim is a dictionary of arrays (one for each plot)
In the plot above it has two arrays.
from matplotlib import pyplot
nrows = len(z_dim) # Number of rows is the number of arrays
My_figure = pyplot.figure(1,(8,8))
my_grid = AxesGrid(My_figure, 111, #Is this always 111?
nrows_ncols = (nrows,1), # Always one column
axes_pad = 0.1,
add_all=True,
share_all=True, # They all share the same time and depth scales
label_mode = "L",
cbar_location="right",
cbar_mode="each",
cbar_size="7%",
cbar_pad="2%",
)
for row_no,parameter in enumerate(z_dim):
ax = my_grid[row_no]
I tried all sorts of things, but finally, by setting aspect=False I got it to work. In the documentation, the table says this defaults to True and the explanation of aspect below says it defaults to False. Although I don’t entirely understand what is going on, I think this threw me off.
So then I had this:
Can you be more specific about which documentation says the default aspect is False? This may need to be fixed. Note that AxesGrid is designed for displaying images with aspect=True. Otherwise, you may better stick to the subplot…
… which looks much better, except that there are two sets of x and y axis
labels? This seems to have something to do with the colorbar. I’ve got:
To me, there is another axes underneath the AxesGrid. It is hard to tell without a complete code.
Now I’m trying to get scales and labels on my colorbars.
I tried:
for i,parameter in enumerate(z_dim):
ax = my_grid[i].pcolor(x_grid,y_grid,z_dim[parameter]) # This is the pcolor plot
my_grid[i].set_ylabel('Depth') # Correctly puts a y label on every plot.
cb = my_grid.cbar_axes[i].colorbar(ax) # Puts in a colorbar for this axes?s
cb.set_ylabel(parameter) #It would be nice if this was on the far right next to the colorbar. I don't see it anywhere. Perhaps underneath something?
The label of the colorbar is set to invisible by default (this is a bug). So, try something like
It looks like perhaps the colorbar axes is inside the ax axes rather than besides it?
In the demo_grid_with_each_cbar example, how would you put a scale and label on the colorbar like in this plot:?
I can put a y_label on each contour plot, but since they all have depth, I’d like to label this only once.
Is there a way to label the entire AxesGrid (or is that subplot?)?
Does label_mode=“1” do what you want?
You may manually make some of the labels invisible.
Please post a “complete”, but simple, script that reproduces your problem. Otherwise, it is hard to track down what is wrong.
Also, please report what version of matplotlib you’re using. The axes_grid toolkit is relatively new and some of the feature may not work in older versions.
Regards,
-JJ
···
On Thu, Dec 3, 2009 at 3:40 PM, Ryan Neve <ryan.neve@…878…287…> wrote:
Thank you very much for your help,
-Ryan
On Wed, Dec 2, 2009 at 10:21 PM, Jae-Joon Lee <lee.j.joon@…287…> wrote:
This happens because, when the AxesGrid is created, gca() is set to the last axes, which is the last colobar axes.
If you use axes_grid toolkit, you’d better not use pyplot command that works on axes. Instead, use axes method directly.
For example, instead of “pyplot.pcolor(…)” , use “ax.pcolor(…)”.
Regards,
-JJ
On Wed, Dec 2, 2009 at 2:18 PM, Ryan Neve <ryan.neve@…287…> wrote:
Hello,
I’m trying to use AxesGrid but I’m running into a problem:
I can plot a single pcolor plot:
But when I try to use AxesGrid, my pcolor plot is ending up where I expect my colorbar to be.
I want to have up to 6 of these plots stacked vertically, sharing a common time axis and y (depth) scale.
I’ll try to simplify my code to show what I’m doing:
I have arrays x_grid and y_grid for time and water depth.
z_dim is a dictionary of arrays (one for each plot)
In the plot above it has two arrays.
from matplotlib import pyplot
nrows = len(z_dim) # Number of rows is the number of arrays
My_figure = pyplot.figure(1,(8,8))
my_grid = AxesGrid(My_figure, 111, #Is this always 111?
nrows_ncols = (nrows,1), # Always one column
axes_pad = 0.1,
add_all=True,
share_all=True, # They all share the same time and depth scales
label_mode = "L",
cbar_location="right",
cbar_mode="each",
cbar_size="7%",
cbar_pad="2%",
)
for row_no,parameter in enumerate(z_dim):
ax = my_grid[row_no]
Name
Default
Description
aspect
True
aspect of axes
then a few lines below:
“aspect
By default (False), widths and heigths of axes in the grid are
scaled independently. If True, they are scaled according to their
data limits (similar to aspect parameter in mpl).”
**Here is a more complete example of my code:**In the following code, x_grid and y_grid are are arrays created by meshgrid and represent time and water depth respectively.
z_dim is a dictionary of one or more arrays of sensor readings corresponding to the depths and times in x_grid and y_grid.
from matplotlib import pyplot
from mpl_toolkits.axes_grid import AxesGrid
nrows = len(z_dim) # Number of rows
DAP_figure = pyplot.figure(1,(20,8))
pyplot.figtext(0.05,.5,“Depth (m)”,rotation=‘vertical’,verticalalignment=‘center’)
Create a grid of axes with the AxesGrid helper class
my_grid = AxesGrid(DAP_figure, 111, # Only one grid in this figure
nrows_ncols = (nrows,1), # one or more rows, but only one column
axes_pad = 0.0, #pad between axes in inches
aspect=False, # If True, all plots are superimposed upon one another.
add_all=True, # not sure why this would ever be False
share_all=True, # I think this means that all axes have the same x & y scales
label_mode = “L”, # labels for depth on left and time on bottom
cbar_location=“right”,
cbar_mode=“each”, # each axes has a different scale
cbar_size=“2%”,
cbar_pad=“1%”,
)
for i,parameter in enumerate(z_dim):
z_dim[parameter] = maskDAP(z_dim[parameter],parameter,dev_mult) #Need to mask NaNs and outliers for each grid
ax = my_grid[i].pcolor(x_grid,y_grid,z_dim[parameter])
my_grid[i].set_ylabel(long_name[parameter]) # Puts a y label on every graph.
my_grid.cbar_axes[i].colorbar(ax)
my_grid.cbar_axes[i].axis[“right”].toggle(ticklabels=True,label=True)
my_grid.cbar_axes[i].set_ylabel(units[parameter]) #Puts the units on the right side of the colorbar
pyplot.draw()
pyplot.show()
I do need a separate colorbar for each plot as they are results of different sensors all taken at the same time and depth scales.
Here is what I have now:
Which, aside from the extra scale labels on the x and y axis is getting close.
Thank You for your help,
-Ryan matplotlib version:
···
On Thu, Dec 3, 2009 at 4:36 PM, Jae-Joon Lee <lee.j.joon@…287…> wrote:
On Thu, Dec 3, 2009 at 3:40 PM, Ryan Neve <ryan.neve@…287…> wrote:
I tried all sorts of things, but finally, by setting aspect=False I got it to work. In the documentation, the table says this defaults to True and the explanation of aspect below says it defaults to False. Although I don’t entirely understand what is going on, I think this threw me off.
So then I had this:
Can you be more specific about which documentation says the default aspect is False? This may need to be fixed. Note that AxesGrid is designed for displaying images with aspect=True. Otherwise, you may better stick to the subplot…
… which looks much better, except that there are two sets of x and y axis
labels? This seems to have something to do with the colorbar. I’ve got:
To me, there is another axes underneath the AxesGrid. It is hard to tell without a complete code.
Now I’m trying to get scales and labels on my colorbars.
I tried:
for i,parameter in enumerate(z_dim):
ax = my_grid[i].pcolor(x_grid,y_grid,z_dim[parameter]) # This is the pcolor plot
my_grid[i].set_ylabel('Depth') # Correctly puts a y label on every plot.
cb = my_grid.cbar_axes[i].colorbar(ax) # Puts in a colorbar for this axes?s
cb.set_ylabel(parameter) #It would be nice if this was on the far right next to the colorbar. I don't see it anywhere. Perhaps underneath something?
The label of the colorbar is set to invisible by default (this is a bug). So, try something like
It looks like perhaps the colorbar axes is inside the ax axes rather than besides it?
In the demo_grid_with_each_cbar example, how would you put a scale and label on the colorbar like in this plot:?
I can put a y_label on each contour plot, but since they all have depth, I’d like to label this only once.
Is there a way to label the entire AxesGrid (or is that subplot?)?
Does label_mode=“1” do what you want?
You may manually make some of the labels invisible.
Please post a “complete”, but simple, script that reproduces your problem. Otherwise, it is hard to track down what is wrong.
Also, please report what version of matplotlib you’re using. The axes_grid toolkit is relatively new and some of the feature may not work in older versions.
Regards,
-JJ
Thank you very much for your help,
-Ryan
On Wed, Dec 2, 2009 at 10:21 PM, Jae-Joon Lee <lee.j.joon@…287…> wrote:
This happens because, when the AxesGrid is created, gca() is set to the last axes, which is the last colobar axes.
If you use axes_grid toolkit, you’d better not use pyplot command that works on axes. Instead, use axes method directly.
For example, instead of “pyplot.pcolor(…)” , use “ax.pcolor(…)”.
Regards,
-JJ
On Wed, Dec 2, 2009 at 2:18 PM, Ryan Neve <ryan.neve@…287…> wrote:
Hello,
I’m trying to use AxesGrid but I’m running into a problem:
I can plot a single pcolor plot:
But when I try to use AxesGrid, my pcolor plot is ending up where I expect my colorbar to be.
I want to have up to 6 of these plots stacked vertically, sharing a common time axis and y (depth) scale.
I’ll try to simplify my code to show what I’m doing:
I have arrays x_grid and y_grid for time and water depth.
z_dim is a dictionary of arrays (one for each plot)
In the plot above it has two arrays.
from matplotlib import pyplot
nrows = len(z_dim) # Number of rows is the number of arrays
My_figure = pyplot.figure(1,(8,8))
my_grid = AxesGrid(My_figure, 111, #Is this always 111?
nrows_ncols = (nrows,1), # Always one column
axes_pad = 0.1,
add_all=True,
share_all=True, # They all share the same time and depth scales
label_mode = "L",
cbar_location="right",
cbar_mode="each",
cbar_size="7%",
cbar_pad="2%",
)
for row_no,parameter in enumerate(z_dim):
ax = my_grid[row_no]
Concerning the documentation, on this page:http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/users/overview.htm it says:
Name
Default
Description
aspect
True
aspect of axes
then a few lines below:
“aspect
By default (False), widths and heigths of axes in the grid are
scaled independently. If True, they are scaled according to their
data limits (similar to aspect parameter in mpl).”
Thanks a lot.
This need to be fixed.
**Here is a more complete example of my code:**In the following code, x_grid and y_grid are are arrays created by meshgrid and represent time and water depth respectively.
z_dim is a dictionary of one or more arrays of sensor readings corresponding to the depths and times in x_grid and y_grid.
Please, “more” complete example does not make any difference unless it is complete.
Try the following code. This is based on the first part of your example. It will show empty axes but without extra ticklabels. Again, I think an extra axes is added to the figure somewhere in your code. And, without a complete, runnable (but simple) code, there is not much I (or others) can help.
"""
This will create a frame for all the sub plots. There will be one row (subplot) per parameter. There will be one column.
All plots will share their x scale (time)
Each row will have its own y scale and legend
"""
from matplotlib import pyplot
from mpl_toolkits.axes_grid import AxesGrid
from numpy import meshgrid, transpose
nrows = len(z_dim) # Number of rows
print('there are',nrows,'rows') # Confirm that the number of rows is as expected.
fig_h_size = 20. # figure width in inches
fig_v_size = 8. # figure height in inches
dev_mult = 3 # How many standard deviations to mask out.
x_grid,y_grid = meshgrid(x_dim,y_dim)
x_grid = transpose(x_grid)
y_grid = transpose(y_grid)
# Start the plotting routines
DAP_figure = pyplot.figure(1,(fig_h_size,fig_v_size))
pyplot.title('Title goes here')
pyplot.figtext(0.05,.5,"Depth (m)",rotation='vertical',verticalalignment='center')
# Create a grid of axes with the AxesGrid helper class
my_grid = AxesGrid(DAP_figure, 111, # Only one grid in DAP_figure
nrows_ncols = (nrows,1),
axes_pad = 0.0, # pad between axes in inches
aspect=False, # By default (False), widths and heigths of axes in the grid are scaled independently. If True, they are scaled according to their data limits
add_all=True, # Add axes to figures if True (default True)
share_all=True, # xaxis & yaxis of all axes are shared if True (default False)
label_mode = "L", # location of tick labels thaw will be displayed. "1" (only the lower left axes), "L" (left most and bottom most axes), or "all"
cbar_location="right", # "right" or "top"
cbar_mode="each", # "None","single", or "each"
cbar_size="2%",
cbar_pad="1%",
)
for i,parameter in enumerate(z_dim):
z_dim[parameter] = maskDAP(z_dim[parameter],parameter,dev_mult) #Need to mask each grid
ax = my_grid[i].pcolor(x_grid,y_grid,z_dim[parameter])
print('from',x_grid[0][0],'to',x_grid[-1][0])
my_grid[i].set_ylabel(long_name[parameter]) # Puts a y label on every graph. Eventually we want this labeled only once.
my_grid.cbar_axes[i].colorbar(ax)
my_grid.cbar_axes[i].axis["right"].toggle(ticklabels=True,label=True)
my_grid.cbar_axes[i].set_ylabel(units[parameter])
# Now show it
pyplot.draw()
pyplot.show()
return x_grid, y_grid, my_grid #Useful only for debugging. There is no code after this.
Here are some typical values for the inpit variables if it helps,
x_dim, time in epoch seconds, is:
array([1253250000, 1253251800, 1253253600, 1253255400, 1253257200,
in the example plot below z_dim is a dictionary with three arrays, ‘do’,‘chl’,‘turb’.
as an example, z_dim[‘chl’] (chlorophyl) is a 2D array of the form:
masked_array(data =
If you want to get some help, you need to take your time to create a simple and complete example (which reproduces the problem) that others can easily test.
Since I believe the problem is due to the existence of an extra axes, your example don’t need to show any images. Please post a simple script that draws a blank AxesGrid and shows extra ticklabels as your current code does.
Regards,
-JJ
···
On Tue, Dec 8, 2009 at 3:10 PM, Ryan Neve <ryan.neve@…287…> wrote:
Sorry for the delay.
I don’t know if I ever included my software versions:
Python & IDLE 2.6.2
matplotlib 0.99.0
numpy 1.4.0rc1 (I was using 1.3.0)
Here is more complete code. This is the only place I use matplotlib for anything so I don’t think any earlier code should affect the plot.
I’ve included the values of the input variables below and I could include all the code which gets the data and manipulates it if this would help.
"""
This will create a frame for all the sub plots. There will be one row (subplot) per parameter. There will be one column.
All plots will share their x scale (time)
Each row will have its own y scale and legend
"""
from matplotlib import pyplot
from mpl_toolkits.axes_grid import AxesGrid
from numpy import meshgrid, transpose
nrows = len(z_dim) # Number of rows
print('there are',nrows,'rows') # Confirm that the number of rows is as expected.
fig_h_size = 20. # figure width in inches
fig_v_size = 8. # figure height in inches
dev_mult = 3 # How many standard deviations to mask out.
x_grid,y_grid = meshgrid(x_dim,y_dim)
x_grid = transpose(x_grid)
y_grid = transpose(y_grid)
# Start the plotting routines
DAP_figure = pyplot.figure(1,(fig_h_size,fig_v_size))
pyplot.title('Title goes here')
pyplot.figtext(0.05,.5,"Depth (m)",rotation='vertical',verticalalignment='center')
# Create a grid of axes with the AxesGrid helper class
my_grid = AxesGrid(DAP_figure, 111, # Only one grid in DAP_figure
nrows_ncols = (nrows,1),
axes_pad = 0.0, # pad between axes in inches
aspect=False, # By default (False), widths and heigths of axes in the grid are scaled independently. If True, they are scaled according to their data limits
add_all=True, # Add axes to figures if True (default True)
share_all=True, # xaxis & yaxis of all axes are shared if True (default False)
label_mode = "L", # location of tick labels thaw will be displayed. "1" (only the lower left axes), "L" (left most and bottom most axes), or "all"
cbar_location="right", # "right" or "top"
cbar_mode="each", # "None","single", or "each"
cbar_size="2%",
cbar_pad="1%",
)
for i,parameter in enumerate(z_dim):
z_dim[parameter] = maskDAP(z_dim[parameter],parameter,dev_mult) #Need to mask each grid
ax = my_grid[i].pcolor(x_grid,y_grid,z_dim[parameter])
print('from',x_grid[0][0],'to',x_grid[-1][0])
my_grid[i].set_ylabel(long_name[parameter]) # Puts a y label on every graph. Eventually we want this labeled only once.
my_grid.cbar_axes[i].colorbar(ax)
my_grid.cbar_axes[i].axis["right"].toggle(ticklabels=True,label=True)
my_grid.cbar_axes[i].set_ylabel(units[parameter])
# Now show it
pyplot.draw()
pyplot.show()
return x_grid, y_grid, my_grid #Useful only for debugging. There is no code after this.
Here are some typical values for the inpit variables if it helps,
x_dim, time in epoch seconds, is:
array([1253250000, 1253251800, 1253253600, 1253255400, 1253257200,
in the example plot below z_dim is a dictionary with three arrays, ‘do’,‘chl’,‘turb’.
as an example, z_dim[‘chl’] (chlorophyl) is a 2D array of the form:
masked_array(data =