python中plot和bar要求的格式不一样_python – matplotlib colorbar tick标签格式

这篇博客探讨了在matplotlib中如何显式设置colorbar的格式,包括颜色梯度、刻度和标签。示例代码展示了如何创建一个带有自定义颜色映射、边界和标签的contourf图,并对colorbar的刻度标签应用特定的数字格式。然而,当尝试设置字体大小和重量时,先前设置的格式字符串失效。博主询问如何在保持格式的同时调整colorbar的字体属性。
摘要由CSDN通过智能技术生成

我想知道如何在matplotlib中显式设置colorbar对象的格式

以下是绘制脚本的示例:

from matplotlib import pyplot

from matplotlib.ticker import MultipleLocator, FormatStrFormatter

from matplotlib.colors import BoundaryNorm

from matplotlib.ticker import MaxNLocator

from pylab import *

import numpy as np

import random

# ----------

plot_aspect = 1.2

plot_height = 10.0

plot_width = int(plot_height*plot_aspect)

# ----------

pyplot.figure(figsize=(plot_width, plot_height), dpi=100)

pyplot.subplots_adjust(left=0.10, right=1.00, top=0.90, bottom=0.06, hspace=0.30)

subplot1 = pyplot.subplot(111)

# ----------

cbar_max = 40.0

cbar_min = 20.0

cbar_step = 1.0

cbar_num_colors = 200

cbar_num_format = "%d"

# ----------

# make random dataset

dx, dy = 5.0, 5.0

y, x = np.mgrid[slice(-100.0, 100.0 + dy, dy),slice(-100.0, 100.0 + dx, dx)]

z = []

for i in x:

z.append([])

for j in y:

z[-1].append(random.uniform(cbar_min,cbar_max))

# ----------

# make random dataset

levels = MaxNLocator(nbins=cbar_num_colors).tick_values(cbar_min, cbar_max)

cmap = pyplot.get_cmap('gist_ncar')

norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True)

pp = pyplot.contourf(x,y,z,levels=levels,cmap=cmap)

cbar = pyplot.colorbar(pp, orientation='vertical', ticks=np.arange(cbar_min, cbar_max+cbar_step, cbar_step), format=cbar_num_format)

cbar.ax.set_ylabel('Color Scale [unit]', fontsize = 16, weight="bold")

CS = pyplot.contour(x,y,z, alpha=0.5)

majorLocator1 = MultipleLocator(10)

majorFormatter1 = FormatStrFormatter('%d')

minorLocator1 = MultipleLocator(5)

subplot1.xaxis.set_major_locator(majorLocator1)

subplot1.xaxis.set_major_formatter(majorFormatter1)

subplot1.xaxis.set_minor_locator(minorLocator1)

pyplot.xticks(fontsize = 16)

pyplot.xlim(-100.0,100.0)

majorLocator2 = MultipleLocator(10)

majorFormatter2 = FormatStrFormatter('%d')

minorLocator2 = MultipleLocator(5)

subplot1.yaxis.set_major_locator(majorLocator2)

subplot1.yaxis.set_major_formatter(majorFormatter2)

subplot1.yaxis.set_minor_locator(minorLocator2)

pyplot.yticks(fontsize = 16)

pyplot.ylim(-100.0,100.0)

subplot1.xaxis.grid()

subplot1.yaxis.grid()

subplot1.axes.set_aspect('equal')

pyplot.suptitle('Main Title', fontsize = 24, weight="bold")

pyplot.xlabel('X [unit]', fontsize=16, weight="bold")

pyplot.ylabel('Y [unit]', fontsize=16, weight="bold")

pyplot.show()

pyplot.close()

这给了我这样的输出:

be7f608f7c64209b2892c5347aeeb5c3.png

目前,colorbar tick标签格式将使用前面提供的格式字符串:cbar_num_format =“%d”,但我还想使用以下方法设置字体大小和重量:

cbar.ax.set_yticklabels(np.arange(cbar_min, cbar_max+cbar_step, cbar_step), fontsize=16, weight='bold')

…但是当我这样做时,先前应用的格式化程序字符串似乎消失了,数字又以“%0.1f”格式返回,而不是我之前应用的“%d”:

pEtTl.jpg

如何以更好的方式防止这种情况发生或控制彩条标记?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值