Python matplotlib画图实例之绘制拥有彩条的图表

yipeiwu_com5年前Python基础

生产定制一个彩条标签。

首先导入:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from numpy.random import randn

制作拥有垂直(默认)彩条的图表:

fig, ax = plt.subplots()

data = np.clip(randn(250, 250), -1, 1)

cax = ax.imshow(data, interpolation='nearest', cmap=cm.coolwarm)
ax.set_title('Gaussian noise with vertical colorbar')

# Add colorbar, make sure to specify tick locations to match desired ticklabels
cbar = fig.colorbar(cax, ticks=[-1, 0, 1])
cbar.ax.set_yticklabels(['< -1', '0', '> 1']) # vertically oriented colorbar

效果图:

制作拥有水平彩条的图表:

fig, ax = plt.subplots()

data = np.clip(randn(250, 250), -1, 1)

cax = ax.imshow(data, interpolation='nearest', cmap=cm.afmhot)
ax.set_title('Gaussian noise with horizontal colorbar')

cbar = fig.colorbar(cax, ticks=[-1, 0, 1], orientation='horizontal')
cbar.ax.set_xticklabels(['Low', 'Medium', 'High']) # horizontal colorbar

plt.show()

效果图:

脚本运行耗时:(0分0.075秒)

总结

以上就是本文关于Python matplotlib画图实例之绘制拥有彩条的图表的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

Django shell调试models输出的SQL语句方法

在settings.py里,配置如下logging: LOGGING = { 'version': 1, 'disable_existing_loggers': False,...

Python获取Linux系统下的本机IP地址代码分享

有时候使用到获取本机IP,就采用以下方式进行。 复制代码 代码如下: #!/usr/bin/python   import socket import struct impor...

python读写csv文件实例代码

Python读取与写入CSV文件需要导入Python自带的CSV模块,然后通过CSV模块中的函数csv.reader()与csv.writer()来进行CSV文件的读取与写入。 写入CS...

SQLite3中文编码 Python的实现

读取十万多条文本写入SQLite类型数据库,由于文本中存在中文字符,插入到数据库没错,取出时一直是UnicodeDecodeError,导致折腾了一天。 最后的解决方法: Python连...

python 字符串和整数的转换方法

数字转成字符串 方法一: 使用格式化字符串: tt=322 tem='%d' %tt tem即为tt转换成的字符串 常用的格式化字符串: %d 整数 %f%F 浮点数 %e%E...