Python matplotlib:如何独立于对象的比例来勾选和勾选 axis/object
Python matplotlib: How to tick and ticklabel an axis/object independently of the object's scale
以下代码在图形中定义一个矩形区域,并根据其中的颜色图绘制颜色条。我已将颜色条的缩放比例更改为其原始值的立方根,但我希望刻度和刻度标签在整个条长度上保持线性,因此它对我的目的来说是正确的。我不妨在那里画一条直线,如果可能的话,它会线性毕业。我该如何处理? mpl 是 matplotlib,plt 是它的 pyplot。这个想法是为彩色世界地图的等值线制作一个彩色条图例。
def draw_legend (clr_map):
""" Draw color bar legend ... (inspired by: http://ramiro.org/notebook/basemap-choropleth/) """
fig = plt.figure(figsize=(8,12))
ax_legend = fig.add_axes([0.26, -0.02, 0.48, 0.016], zorder=3)
grads = np.linspace(0.,1.,400)
bins = np.linspace(0.,1.,10)
scheme = [clr_map(i/400) for i in range(400)]
cmap = mpl.colors.ListedColormap(scheme)
cb = mpl.colorbar.ColorbarBase(ax_legend, cmap=cmap, ticks=bins, boundaries=grads**(1/3.), \
orientation='horizontal') # Before I'd done cmap as sqrt(): boundaries=np.sqrt(grads)
#cb.ax.set_xticks(bins)
cb.ax.set_xticklabels([str(round(i, 1)) for i in bins], fontsize=10);
draw_legend (plt.cm.plasma)
要在颜色条上设置刻度,您可以使用 cb.set_ticks(bins**(1/3.))
。您也可以直接缩放颜色图 (clr_map((i/400.)**(1./3))
)。
import matplotlib.pyplot as plt
import matplotlib.colors
import matplotlib.colorbar
import numpy as np
def draw_legend (clr_map):
""" Draw a color bar legend
with a qubic root colormap
"""
# Version 1, scale boundaries, set ticks to colorbar values
fig = plt.figure(figsize=(6,4))
ax_legend = fig.add_axes([0.26, 0.7, 0.48, 0.1], zorder=3)
ax_legend.set_title("Version 1\nscale boundaries, set ticks to colorbar values")
grads = np.linspace(0.,1.,400)
bins = np.linspace(0.,1.,11)
scheme = [clr_map(i/400.) for i in range(400)]
cmap = matplotlib.colors.ListedColormap(scheme)
cb = matplotlib.colorbar.ColorbarBase(ax_legend, cmap=cmap, ticks=bins, boundaries=grads**(1/3.), \
orientation='horizontal')
cb.set_ticks(bins**(1/3.))
cb.ax.set_xticklabels(bins**(1/3.), fontsize=10, rotation =45, ha="center")
cb.draw_all()
# Version 2, scale colormap, set ticks to arbitrary values
ax_legend2 = fig.add_axes([0.26, 0.27, 0.48, 0.1], zorder=3)
ax_legend2.set_title("Version 2\nscale colormap, set ticks to arbitrary values")
grads = np.linspace(0.,1.,400)
bins = np.linspace(0.,1.,11)
scheme = [clr_map((i/400.)**(1./3)) for i in range(400)]
cmap = matplotlib.colors.ListedColormap(scheme)
cb = matplotlib.colorbar.ColorbarBase(ax_legend2, cmap=cmap, ticks=bins,
orientation='horizontal')
cb.set_ticks(bins)
cb.draw_all()
draw_legend (plt.cm.jet)
plt.savefig(__file__+".png")
plt.show()
以下代码在图形中定义一个矩形区域,并根据其中的颜色图绘制颜色条。我已将颜色条的缩放比例更改为其原始值的立方根,但我希望刻度和刻度标签在整个条长度上保持线性,因此它对我的目的来说是正确的。我不妨在那里画一条直线,如果可能的话,它会线性毕业。我该如何处理? mpl 是 matplotlib,plt 是它的 pyplot。这个想法是为彩色世界地图的等值线制作一个彩色条图例。
def draw_legend (clr_map):
""" Draw color bar legend ... (inspired by: http://ramiro.org/notebook/basemap-choropleth/) """
fig = plt.figure(figsize=(8,12))
ax_legend = fig.add_axes([0.26, -0.02, 0.48, 0.016], zorder=3)
grads = np.linspace(0.,1.,400)
bins = np.linspace(0.,1.,10)
scheme = [clr_map(i/400) for i in range(400)]
cmap = mpl.colors.ListedColormap(scheme)
cb = mpl.colorbar.ColorbarBase(ax_legend, cmap=cmap, ticks=bins, boundaries=grads**(1/3.), \
orientation='horizontal') # Before I'd done cmap as sqrt(): boundaries=np.sqrt(grads)
#cb.ax.set_xticks(bins)
cb.ax.set_xticklabels([str(round(i, 1)) for i in bins], fontsize=10);
draw_legend (plt.cm.plasma)
要在颜色条上设置刻度,您可以使用 cb.set_ticks(bins**(1/3.))
。您也可以直接缩放颜色图 (clr_map((i/400.)**(1./3))
)。
import matplotlib.pyplot as plt
import matplotlib.colors
import matplotlib.colorbar
import numpy as np
def draw_legend (clr_map):
""" Draw a color bar legend
with a qubic root colormap
"""
# Version 1, scale boundaries, set ticks to colorbar values
fig = plt.figure(figsize=(6,4))
ax_legend = fig.add_axes([0.26, 0.7, 0.48, 0.1], zorder=3)
ax_legend.set_title("Version 1\nscale boundaries, set ticks to colorbar values")
grads = np.linspace(0.,1.,400)
bins = np.linspace(0.,1.,11)
scheme = [clr_map(i/400.) for i in range(400)]
cmap = matplotlib.colors.ListedColormap(scheme)
cb = matplotlib.colorbar.ColorbarBase(ax_legend, cmap=cmap, ticks=bins, boundaries=grads**(1/3.), \
orientation='horizontal')
cb.set_ticks(bins**(1/3.))
cb.ax.set_xticklabels(bins**(1/3.), fontsize=10, rotation =45, ha="center")
cb.draw_all()
# Version 2, scale colormap, set ticks to arbitrary values
ax_legend2 = fig.add_axes([0.26, 0.27, 0.48, 0.1], zorder=3)
ax_legend2.set_title("Version 2\nscale colormap, set ticks to arbitrary values")
grads = np.linspace(0.,1.,400)
bins = np.linspace(0.,1.,11)
scheme = [clr_map((i/400.)**(1./3)) for i in range(400)]
cmap = matplotlib.colors.ListedColormap(scheme)
cb = matplotlib.colorbar.ColorbarBase(ax_legend2, cmap=cmap, ticks=bins,
orientation='horizontal')
cb.set_ticks(bins)
cb.draw_all()
draw_legend (plt.cm.jet)
plt.savefig(__file__+".png")
plt.show()