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

yipeiwu_com6年前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画图实例之绘制拥有彩条的图表的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

Python从文件中读取数据的方法讲解

编写了一个名为learning_python.txt的文件,内容如下: [root@centos7 tmp]# cat learning_python.txt In Python...

python中pika模块问题的深入探究

python中pika模块问题的深入探究

前言 工作中经常用到rabbitmq,而用的语言主要是python,所以也就经常会用到python中的pika模块,但是这个模块的使用,也给我带了很多问题,这里整理一下关于这个模块我在使...

在Python中字符串、列表、元组、字典之间的相互转换

在Python中字符串、列表、元组、字典之间的相互转换

一、字符串(str) 字符串转换为列表 使用list()方法 str_1 = "1235" str_2 = 'zhangsan' str_3 = '''lisi''' tuple_1...

解决Django一个表单对应多个按钮的问题

需求: 在django中,有时候我们需要在一个表单中设置多个按钮实现不同的功能。 解决方法: 为不同按钮添加不同name属性,然后再后台判断name值。python2环境下,例如: 我们...

基于Pandas读取csv文件Error的总结

OSError:报错1 <span style="font-size:14px;">pandas\_libs\parsers.pyx in pandas._libs.pa...