本文整理汇总了Python中matplotlib.colorbar方法的典型用法代码示例。如果您正苦于以下问题:Python matplotlib.colorbar方法的具体用法?Python matplotlib.colorbar怎么用?Python matplotlib.colorbar使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类matplotlib
的用法示例。
在下文中一共展示了matplotlib.colorbar方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: plotTZ
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import colorbar [as 别名]
def plotTZ(filename=None):
t = np.linspace(0, 1, 101)
z = 0.25 + 0.5 / (1 + np.exp(- 20 * (t - 0.5))) + 0.05 * np.cos(t * 2 * np.pi)
cmap = cm.get_cmap('cool')
fig, (ax1, ax2) = plt.subplots(1, 2, gridspec_kw = {'width_ratios':[19, 1]})
poly1 = [[0, 0]]
poly1.extend([[t[i], z[i]] for i in range(t.size)])
poly1.extend([[1, 0], [0, 0]])
poly2 = [[0, 1]]
poly2.extend([[t[i], z[i]] for i in range(t.size)])
poly2.extend([[1, 1], [0, 1]])
poly1 = plt.Polygon(poly1,fc=cmap(0.0))
poly2 = plt.Polygon(poly2,fc=cmap(1.0))
ax1.add_patch(poly1)
ax1.add_patch(poly2)
ax1.set_xlabel('x1', size=22)
ax1.set_ylabel('x2', size=22)
ax1.set_title('True Data', size=28)
colorbar.ColorbarBase(ax2, cmap=cmap, format='%.1f')
ax2.set_ylabel('Output y', size=22)
plt.show()
if not filename is None:
plt.savefig(filename, format="pdf", bbox_inches="tight")
plt.close()
示例2: plotTZ
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import colorbar [as 别名]
def plotTZ(filename=None):
cmap = cm.get_cmap('cool')
fig, (ax1, ax2) = plt.subplots(1, 2, gridspec_kw = {'width_ratios':[19, 1]})
ax1.add_patch(pl.Rectangle(xy=[0, 0], width=0.5, height=0.5, facecolor=cmap(0.0), linewidth='2.0'))
ax1.add_patch(pl.Rectangle(xy=[0.5, 0.5], width=0.5, height=0.5, facecolor=cmap(0.0), linewidth='2.0'))
ax1.add_patch(pl.Rectangle(xy=[0, 0.5], width=0.5, height=0.5, facecolor=cmap(1.0), linewidth='2.0'))
ax1.add_patch(pl.Rectangle(xy=[0.5, 0], width=0.5, height=0.5, facecolor=cmap(1.0), linewidth='2.0'))
ax1.set_xlabel('x1', size=22)
ax1.set_ylabel('x2', size=22)
ax1.set_title('True Data', size=28)
colorbar.ColorbarBase(ax2, cmap=cmap, format='%.1f')
ax2.set_ylabel('Output y', size=22)
plt.show()
if not filename is None:
plt.savefig(filename, format="pdf", bbox_inches="tight")
plt.close()
示例3: _plot_colorbar
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import colorbar [as 别名]
def _plot_colorbar(self, color_legend_ax: Axes, normalize):
"""
Plots a horizontal colorbar given the ax an normalize values
Parameters
----------
color_legend_ax
normalize
Returns
-------
None, updates color_legend_ax
"""
cmap = pl.get_cmap(self.cmap)
import matplotlib.colorbar
matplotlib.colorbar.ColorbarBase(
color_legend_ax, orientation='horizontal', cmap=cmap, norm=normalize
)
color_legend_ax.set_title(self.color_legend_title, fontsize='small')
color_legend_ax.xaxis.set_tick_params(labelsize='small')
示例4: plotRule
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import colorbar [as 别名]
def plotRule(mdl, X, d1, d2, alpha=0.8, filename='', rnum=-1, plot_line=[]):
cmap = cm.get_cmap('cool')
fig, (ax1, ax2) = plt.subplots(1, 2, gridspec_kw = {'width_ratios':[19, 1]})
if rnum <= 0:
rnum = len(mdl.rule_)
else:
rnum = min(len(mdl.rule_), rnum)
idx = np.argsort(mdl.weight_[:rnum])
for i in range(rnum):
r = mdl.rule_[idx[i]]
box, vmin, vmax = __r2boxWithX(r, X)
if mdl.modeltype_ == 'regression':
c = cmap(mdl.pred_[idx[i]])
elif mdl.modeltype_ == 'classification':
r = mdl.pred_[idx[i]] / max(np.unique(mdl.pred_).size - 1, 1)
c = cmap(r)
ax1.add_patch(pl.Rectangle(xy=[box[0, d1], box[0, d2]], width=(box[1, d1] - box[0, d1]), height=(box[1, d2] - box[0, d2]), facecolor=c, linewidth='2.0', alpha=alpha))
if len(plot_line) > 0:
for l in plot_line:
ax1.plot(l[0], l[1], 'k--')
ax1.set_xlabel('x1', size=22)
ax1.set_ylabel('x2', size=22)
ax1.set_title('Simplified Model (K = %d)' % (rnum,), size=28)
colorbar.ColorbarBase(ax2, cmap=cmap, format='%.1f')
ax2.set_ylabel('Predictor y', size=22)
plt.show()
if not filename == '':
plt.savefig(filename, format="pdf", bbox_inches="tight")
plt.close()
示例5: plotEachRule
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import colorbar [as 别名]
def plotEachRule(mdl, X, d1, d2, alpha=0.8, filename='', rnum=-1, plot_line=[]):
if rnum <= 0:
rnum = len(mdl.rule_)
else:
rnum = min(len(mdl.rule_), rnum)
m = rnum // 4
if m * 4 < rnum:
m += 1
cmap = cm.get_cmap('cool')
fig, ax = plt.subplots(m, 4 + 1, figsize=(4 * 4, 3 * m), gridspec_kw = {'width_ratios':[15, 15, 15, 15, 1]})
idx = np.argsort(mdl.weight_[:rnum])
for i in range(rnum):
j = i // 4
k = i - 4 * j
r = mdl.rule_[idx[i]]
box, vmin, vmax = __r2boxWithX(r, X)
if mdl.modeltype_ == 'regression':
c = cmap(mdl.pred_[idx[i]])
elif mdl.modeltype_ == 'classification':
r = mdl.pred_[idx[i]] / max(np.unique(mdl.pred_).size - 1, 1)
c = cmap(r)
ax[j, k].add_patch(pl.Rectangle(xy=[box[0, d1], box[0, d2]], width=(box[1, d1] - box[0, d1]), height=(box[1, d2] - box[0, d2]), facecolor=c, linewidth='2.0', alpha=alpha))
if len(plot_line) > 0:
for l in plot_line:
ax[j, k].plot(l[0], l[1], 'k--')
ax[j, k].set_xlim([0, 1])
ax[j, k].set_ylim([0, 1])
if k == 3:
cbar = colorbar.ColorbarBase(ax[j, -1], cmap=cmap, format='%.1f', ticks=[0.0, 0.5, 1.0])
cbar.ax.set_yticklabels([0.0, 0.5, 1.0])
ax[j, -1].set_ylabel('Predictor y', size=12)
plt.show()
if not filename == '':
plt.savefig(filename, format="pdf", bbox_inches="tight")
plt.close()
示例6: colorbar
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import colorbar [as 别名]
def colorbar(mappable=None, cax=None, ax=None, **kw):
if mappable is None:
mappable = gci()
if mappable is None:
raise RuntimeError('No mappable was found to use for colorbar '
'creation. First define a mappable such as '
'an image (with imshow) or a contour set ('
'with contourf).')
if ax is None:
ax = gca()
ret = gcf().colorbar(mappable, cax = cax, ax=ax, **kw)
draw_if_interactive()
return ret
示例7: _colorbar_extension_shape
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import colorbar [as 别名]
def _colorbar_extension_shape(spacing):
'''
Produce 4 colorbars with rectangular extensions for either uniform
or proportional spacing.
Helper function for test_colorbar_extension_shape.
'''
# Get a colormap and appropriate norms for each extension type.
cmap, norms = _get_cmap_norms()
# Create a figure and adjust whitespace for subplots.
fig = plt.figure()
fig.subplots_adjust(hspace=4)
for i, extension_type in enumerate(('neither', 'min', 'max', 'both')):
# Get the appropriate norm and use it to get colorbar boundaries.
norm = norms[extension_type]
boundaries = values = norm.boundaries
# Create a subplot.
cax = fig.add_subplot(4, 1, i + 1)
# Turn off text and ticks.
for item in cax.get_xticklabels() + cax.get_yticklabels() +\
cax.get_xticklines() + cax.get_yticklines():
item.set_visible(False)
# Generate the colorbar.
cb = ColorbarBase(cax, cmap=cmap, norm=norm,
boundaries=boundaries, values=values,
extend=extension_type, extendrect=True,
orientation='horizontal', spacing=spacing)
# Return the figure to the caller.
return fig
示例8: _colorbar_extension_length
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import colorbar [as 别名]
def _colorbar_extension_length(spacing):
'''
Produce 12 colorbars with variable length extensions for either
uniform or proportional spacing.
Helper function for test_colorbar_extension_length.
'''
# Get a colormap and appropriate norms for each extension type.
cmap, norms = _get_cmap_norms()
# Create a figure and adjust whitespace for subplots.
fig = plt.figure()
fig.subplots_adjust(hspace=.6)
for i, extension_type in enumerate(('neither', 'min', 'max', 'both')):
# Get the appropriate norm and use it to get colorbar boundaries.
norm = norms[extension_type]
boundaries = values = norm.boundaries
for j, extendfrac in enumerate((None, 'auto', 0.1)):
# Create a subplot.
cax = fig.add_subplot(12, 1, i*3 + j + 1)
# Turn off text and ticks.
for item in cax.get_xticklabels() + cax.get_yticklabels() +\
cax.get_xticklines() + cax.get_yticklines():
item.set_visible(False)
# Generate the colorbar.
cb = ColorbarBase(cax, cmap=cmap, norm=norm,
boundaries=boundaries, values=values,
extend=extension_type, extendfrac=extendfrac,
orientation='horizontal', spacing=spacing)
# Return the figure to the caller.
return fig
示例9: test_colorbar_extension_shape
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import colorbar [as 别名]
def test_colorbar_extension_shape():
'''Test rectangular colorbar extensions.'''
# Create figures for uniform and proportionally spaced colorbars.
fig1 = _colorbar_extension_shape('uniform')
fig2 = _colorbar_extension_shape('proportional')
示例10: test_colorbar_extension_length
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import colorbar [as 别名]
def test_colorbar_extension_length():
'''Test variable length colorbar extensions.'''
# Create figures for uniform and proportionally spaced colorbars.
fig1 = _colorbar_extension_length('uniform')
fig2 = _colorbar_extension_length('proportional')
示例11: test_colorbar_single_scatter
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import colorbar [as 别名]
def test_colorbar_single_scatter():
# Issue #2642: if a path collection has only one entry,
# the norm scaling within the colorbar must ensure a
# finite range, otherwise a zero denominator will occur in _locate.
plt.figure()
x = np.arange(4)
y = x.copy()
z = np.ma.masked_greater(np.arange(50, 54), 50)
cmap = plt.get_cmap('jet', 16)
cs = plt.scatter(x, y, z, c=z, cmap=cmap)
plt.colorbar(cs)
示例12: _test_remove_from_figure
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import colorbar [as 别名]
def _test_remove_from_figure(use_gridspec):
"""
Test `remove_from_figure` with the specified ``use_gridspec`` setting
"""
fig = plt.figure()
ax = fig.add_subplot(111)
sc = ax.scatter([1, 2], [3, 4], cmap="spring")
sc.set_array(np.array([5, 6]))
pre_figbox = np.array(ax.figbox)
cb = fig.colorbar(sc, use_gridspec=use_gridspec)
fig.subplots_adjust()
cb.remove()
fig.subplots_adjust()
post_figbox = np.array(ax.figbox)
assert (pre_figbox == post_figbox).all()
示例13: test_remove_from_figure_with_gridspec
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import colorbar [as 别名]
def test_remove_from_figure_with_gridspec():
"""
Make sure that `remove_from_figure` removes the colorbar and properly
restores the gridspec
"""
_test_remove_from_figure(True)
示例14: test_remove_from_figure_no_gridspec
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import colorbar [as 别名]
def test_remove_from_figure_no_gridspec():
"""
Make sure that `remove_from_figure` removes a colorbar that was created
without modifying the gridspec
"""
_test_remove_from_figure(False)
示例15: colorbar
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import colorbar [as 别名]
def colorbar(mappable=None, cax=None, ax=None, **kw):
if mappable is None:
mappable = gci()
if mappable is None:
raise RuntimeError('No mappable was found to use for colorbar '
'creation. First define a mappable such as '
'an image (with imshow) or a contour set ('
'with contourf).')
if ax is None:
ax = gca()
ret = gcf().colorbar(mappable, cax = cax, ax=ax, **kw)
return ret