Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a colour bar for a plot made with plt.fill
    primarykey
    data
    text
    <p>I'm new to Python (was an IDL user before hand) so I hope that I'm asking this in an understandable way. I've been trying to create a polar plot with x number of bins where the data in the bin is averaged and given a colour associated with that value. This seems to work fine while using the plt.fill command where I can define the bin and then the fill colour. The problem comes when I then try to make a colour bar to go with it. I keep getting errors that state AttributeError: 'Figure' object has no attribute 'autoscale_None'</p> <p>Any advice would be helpful thanks.</p> <pre><code>import matplotlib.pyplot as plt import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.cm as cm from matplotlib.pyplot import figure, show, rc, grid import pylab r = np.arange(50)/5. rstep = r[1] - r[0] theta = np.arange(50)/50.*2.*np.pi tstep = theta[1] - theta[0] colorv = np.arange(50)/50. # force square figure and square axes looks better for polar, IMO width, height = mpl.rcParams['figure.figsize'] size = min(width, height) # make a square figure fig = figure(figsize=(size, size)) ax = fig.add_axes([0.1, 0.1, .8, .8])#, polar=True) my_cmap = cm.jet for j in range(len(r)): rbox = np.array([r[j], r[j], r[j]+ rstep, r[j] + rstep]) for i in range(len(theta)): thetabox = np.array([theta[i], theta[i] + tstep, theta[i] + tstep, theta[i]]) x = rbox*np.cos(thetabox) y = rbox*np.sin(thetabox) plt.fill(x,y, facecolor = my_cmap(colorv[j])) # Add colorbar, make sure to specify tick locations to match desired ticklabels cbar = fig.colorbar(fig, ticks=[np.min(colorv), np.max(colorv)]) cb = plt.colorbar() plt.show() </code></pre> <p><strong>*</strong> here is a slightly better example of my real data, there are holes missing everywhere, so in this example I've just made a big one in a quarter of the circle. When I've tried meshing, the code seems to try to interpolate over these regions. </p> <pre><code>r = np.arange(50)/50.*7. + 3. rstep = r[1] - r[0] theta = np.arange(50)/50.*1.5*np.pi - np.pi tstep = theta[1] - theta[0] colorv = np.sin(r/10.*np.pi) # force square figure and square axes looks better for polar, IMO width, height = mpl.rcParams['figure.figsize'] size = min(width, height) # make a square figure fig = figure(figsize=(size, size)) ax = fig.add_axes([0.1, 0.1, .8, .8])#, polar=True) my_cmap = cm.jet for j in range(len(r)): rbox = np.array([r[j], r[j], r[j]+ rstep, r[j] + rstep]) for i in range(len(theta)): thetabox = np.array([theta[i], theta[i] + tstep, theta[i] + tstep, theta[i]]) x = rbox*np.cos(thetabox) y = rbox*np.sin(thetabox) plt.fill(x,y, facecolor = my_cmap(colorv[j])) # Add colorbar, make sure to specify tick locations to match desired ticklabels #cbar = fig.colorbar(fig, ticks=[np.min(colorv), np.max(colorv)]) #cb = plt.colorbar() plt.show() </code></pre> <p>And then with a meshing involved... </p> <p>from matplotlib.mlab import griddata</p> <pre><code>r = np.arange(50)/5. rstep = r[1] - r[0] theta = np.arange(50)/50.*1.5*np.pi - np.pi tstep = theta[1] - theta[0] colorv = np.sin(r/10.*np.pi) # force square figure and square axes looks better for polar, IMO width, height = mpl.rcParams['figure.figsize'] size = min(width, height) # make a square figure fig = figure(figsize=(size, size)) ax = fig.add_axes([0.1, 0.1, .8, .8])#, polar=True) my_cmap = cm.jet x = r*np.cos(theta) y = r*np.sin(theta) X,Y = np.meshgrid(x,y) data = griddata(x,y,colorv,X,Y) cax = plt.contourf(X,Y, data) plt.colorbar() # Add colorbar, make sure to specify tick locations to match desired ticklabels #cbar = fig.colorbar(fig, ticks=[np.min(colorv), np.max(colorv)]) #cb = plt.colorbar() plt.show() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload