在Python中格式化数字以在plt.colorbar()上使用逗号 [英] Formatting Numbers to have a comma on plt.colorbar() in Python

查看:28
本文介绍了在Python中格式化数字以在plt.colorbar()上使用逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置颜色栏的格式,以使数字使用逗号格式化.任何帮助将不胜感激

 将numpy导入为np导入matplotlib.pyplot作为pltplt.matshow(np.array(([[30000,8000],[12000,25000])))plt.colorbar() 

解决方案

您可以创建并指定

I am trying to format my colorbar such the numbers are formatted with commas. Any help would be greatly appreciated

import numpy as np
import matplotlib.pyplot as plt

plt.matshow(np.array(([30000,8000],[12000,25000])))
plt.colorbar()

解决方案

You can create and specify a custom formatter:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter

comma_fmt = FuncFormatter(lambda x, p: format(int(x), ','))

plt.matshow(np.array(([30000,8000],[12000,25000])))
plt.colorbar(format=comma_fmt)

plt.show()

这篇关于在Python中格式化数字以在plt.colorbar()上使用逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆