网络编程
位置:首页>> 网络编程>> Python编程>> Python matplotlib画图实例之绘制拥有彩条的图表

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

作者:mengwei  发布时间:2023-01-26 02:06:25 

标签:python,matplotlib,实例

生产定制一个彩条标签。

首先导入:


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

效果图:

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

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


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()

效果图:

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

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

来源:https://matplotlib.org/index.html

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com