我有一个使用 matplotlib 生成的轮廓图。
import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(-10, 10, 100)
Y = np.linspace(-10, 10, 100)
XX, YY = np.meshgrid(X, Y)
ZZ = XX**2 + YY**2
levels = np.linspace(0, 200, 10)
fig, ax = plt.subplots()
cf = ax.contourf(X, Y, ZZ, levels=levels)
cbar = fig.colorbar(cf)
Run Code Online (Sandbox Code Playgroud)
这会生成这个数字:
现在想象一下,由于某种原因,这个数字是通过一些功能的调用交给用户,用户只得到了ax
,fig
和cbar
对象。
更新:我想这样做的原因是因为我必须以自动化的方式制作大量的等高线图。我已经在后端自动创建了轮廓图(在上面模糊地描述为“函数调用”)。但是对于某些情节,需要稍作修改才能生成精美的情节。因此,而不是操纵备份或者不必增加大量数额的每一种情况的选择,这将是很好,如果这对夫妻的我需要调整的数字+400出来的数字通过可调ax
,fig
和cbar
对象。
现在只操作这些对象,我想放大,并重新调整颜色条和级别。
放大:
ax.set_xlim(2.5, 7.5)
ax.set_ylim(2.5, 7.5)
Run Code Online (Sandbox Code Playgroud)
更改颜色条和刻度上的限制:
new_clim = (0, 100)
# find location of the new upper limit on the color bar
loc_on_cbar = cbar.norm(new_clim[1])
# redefine limits of the colorbar
cf.colorbar.set_clim(*new_clim)
cf.colorbar.set_ticks(np.linspace(*new_clim, 10))
# redefine the limits of the levels of the contour
cf.set_clim(*new_clim)
# updating the contourplot
cf.changed()
Run Code Online (Sandbox Code Playgroud)
更改颜色条本身的比例:
cbar.ax.set_ylim(0, loc_on_cbar)
cbar.ax.autoscale_view()
Run Code Online (Sandbox Code Playgroud)
现在这里有一些问题:
非常感谢!
归档时间: |
|
查看次数: |
1094 次 |
最近记录: |