【可视化】matplotlib_颜色_渐变_色块

本文主要介绍matplotlib中单一颜色的使用、渐变色和色块的定义和使用,渐变色块和线条等。

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib import cm 
from matplotlib.colors import ListedColormap, LinearSegmentedColormap

单一颜色使用方法

matplotlib中颜色对照可以查看:https://blog.csdn.net/qq_44901949/article/details/124738392
颜色选择可以查看:https://color.adobe.com/zh/create
# 颜色可以通过16进制颜色代码 常用颜色编号 颜色名称 RGBA进行调用
ts = np.random.randint(1,100,20)
ts2 = np.random.randint(1,50,20)
plt.plot(ts,c = '#17ADF2')  # 使用16进制颜色代码 
plt.plot(ts2,c = 'b')  # 使用常用颜色代码 r b 等 
plt.plot(np.random.randint(50,100,20),c = 'tomato')  # 使用颜色名称 

在这里插入图片描述

# 使用RBGA需要将数值控制在0-1之间 
def set_color(R,G,B,alpha):
    return np.array([R,G,B,255*alpha])/255
plt.hist(ts2,color= set_color(169,217,148,1))

在这里插入图片描述

渐变颜色条

# 渐变颜色可以从cm中导入
cm.OrRd
OrRd
OrRd colormap
under
bad
over
# 渐变条颜色为从0-1的分布 cm.OrRd(x) 可提取中间点某一位置颜色
plt.plot(ts2,c =cm.OrRd(0.6))
plt.plot(ts,c =cm.OrRd(0.9))

在这里插入图片描述

# 使用cmap和c两个参数创建散点图
plt.scatter(x,ts,c = ts2/100,s = ts2 *10,cmap=cm.OrRd,alpha=0.8)
plt.title('1')

在这里插入图片描述

# 增加颜色条
plt.scatter(x,ts,c = ts2/100,s = ts2 *10,cmap=cm.BuGn,alpha=0.8)
plt.title('1')
plt.colorbar(
#     orientation='horizontal'
)

在这里插入图片描述

# 使用 LinearSegmentedColormap 通过列表自定义渐变条
colors = ["w", "tomato"]
cmap1 = LinearSegmentedColormap.from_list("mycmap", colors)
cmap1 
mycmap
mycmap colormap
under
bad
over
colors = ['#10454F','tomato','#818274']
cmap1 = LinearSegmentedColormap.from_list("mycmap", colors)
cmap1 
mycmap
mycmap colormap
under
bad
over

色阶块

cm.tab20c
tab20c
tab20c colormap
under
bad
over
# 不同色块的柱状图
plt.bar(x = np.arange(5),height= np.random.randint(1,10,5),color = [cm.tab20c(i) for i in range(5)])

在这里插入图片描述

# 自定义色块
ListedColormap(['#10454F','#506266','#818274','#A3AB78'])
from_list
from_list colormap
under
bad
over

渐变图形

# imshow 创建一个渐变色块
a = np.array([[1, 1],
              [2, 2]])
plt.imshow(a, interpolation='bicubic', cmap=plt.cm.Blues_r)

在这里插入图片描述

# 利用imshow创建渐变的图形 
fig, ax = plt.subplots()
ax.set(xlim=(0,10), ylim=(0,1), autoscale_on=False)
N = 10
x = np.arange(N) + 0.15
y = np.random.rand(N)
width = 0.4
for x, y in zip(x, y):
    ax.imshow(a, interpolation='bicubic', extent=(x, x+width, 0, y), cmap=plt.cm.Wistia)
ax.set_aspect('auto')
plt.show()

在这里插入图片描述

fig, ax = plt.subplots()
ax.set(xlim=(0,8), ylim=(0,4))
ax.imshow(X = np.array([[1],
              [2]]),interpolation='bicubic',cmap=cmap1, extent=(0.5, 1.5, 0, 2) )
ax.imshow(X = np.array([[1,2],[1,2]]),interpolation='bicubic',cmap=cm.Blues_r, extent=(1.5, 8, 2, 4) )
ax.imshow(X = np.array([[1,],[1,]]),interpolation='bicubic',cmap=cm.tab20c, extent=(0.5, 1.5, 2, 4) )

在这里插入图片描述

渐变线条

# 使用散点图形成渐变线条 
x = np.arange(0,10,0.01)
y = np.sin(x)
cmap2 = LinearSegmentedColormap.from_list('22',[cmap1(0.8),cmap1(0.5)])
cmap2
22
22 colormap
under
bad
over
plt.scatter(x,y,c=x/10,cmap=cmap2,s=(10-x)*3)

在这里插入图片描述


  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: matplotlib_inline 是一种使用 Jupyter Notebook 中的 IPython 内联后端的 matplotlib 渲染器。它允许在笔记本中嵌入 matplotlib 绘图并实时查看结果,而无需创建单独的窗口。 ### 回答2: matplotlib_inline 是 Python 中的一个库,用于在 Jupyter Notebook 中内嵌显示 matplotlib 的图形。它可以方便地在笔记本中直接输出图形结果,而不需要额外的操作或显示窗口。使用 matplotlib_inline,我们可以更加方便地进行数据分析、可视化和交互式数据探索。 在 Jupyter Notebook 中使用 matplotlib_inline,只需要在代码中导入 matplotlib 并设置 `%matplotlib inline`。这样,每次绘图操作都会直接输出图形,而不需要再额外使用 `plt.show()` 命令显示图像。 使用 matplotlib_inline 有许多好处。首先,它使代码更加整洁简单,减少了不必要的代码行数。此外,它还提供了更快速的图形显示速度,可以快速查看数据可视化结果。同时,matplotlib_inline 还支持交互式操作,可以通过鼠标滚轮或其他手势对图像进行缩放、平移等操作,方便进行数据探索和分析。 总之,matplotlib_inline 是一个方便快捷的工具,使得在 Jupyter Notebook 中使用 matplotlib 进行数据可视化更加高效和直观。它大大简化了数据科学工作流程,使得数据分析工作更加快速、便捷。 ### 回答3: matplotlib_inline是一种用于Jupyter Notebook和JupyterLab中的Pythonmatplotlib的后端。matplotlib是一个功能强大的数据可视化工具,而matplotlib_inline则允许在Notebook中内联地展示图形,而不需要在独立的窗口中显示。 matplotlib_inline能够让我们在Notebook中轻松地创建、修改和展示各种类型的图表,例如折线图、散点图、柱状图等。它提供了简单的API,方便我们进行图表的配置和样式设置。 使用matplotlib_inline时,我们可以通过在Notebook的单元格中运行代码来创建图表,并且它会直接在输出区域显示图像。这样的好处是我们可以在Notebook中实时交互式地修改图表参数和数据,使得我们能够更方便地进行数据的可视化和分析。 另外,matplotlib_inline还支持在Notebook中使用一些特殊的魔术命令,例如%matplotlib inline,用于激活matplotlib_inline后端。这样一来,只需一行代码,就可以将matplotlib_inline集成到Notebook中。 总而言之,matplotlib_inline是一个方便易用的工具,使得在Jupyter Notebook中进行数据可视化变得简单而快捷,为我们提供了更流畅的数据分析体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值