简体   繁体   English

使用 Matplotlib 基于数组的线条颜色代码

[英]Color code of lines based on an array using Matplotlib

I am drawing multiple horizontal and vertical lines using ax.hlines() and ax.vlines() respectively.我分别使用ax.hlines()ax.vlines()绘制多条水平和垂直线。 I want to assign values to these lines using the array P and the order of assignment is presented in the expected output.我想使用数组P为这些行分配值,并且分配顺序显示在预期的输出中。

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import numpy as np

fig,ax = plt.subplots(1)
n=3
for i in range(0,n):
    for j in range(0,n):
        rect = mpl.patches.Rectangle((200+200*i,200+200*j),10*n, 10*n, linewidth=1, edgecolor='black', facecolor='black')
        ax.add_patch(rect)
        ax.hlines(200+200*i+5*n, 200, 200*n, zorder=0)
        ax.vlines(200+200*j+5*n, 200, 200*n, zorder=0)

ax.set_xlim(left = 0, right = 220*n)
ax.set_ylim(bottom = 0, top = 220*n)
plt.show()

#########################################
P=np.array([[1.9],
       [4.9],
       [6.1],
       [8.2],
       [1.8],
       [5.8],
       [9.7],
       [7.3],
       [8.9],
       [2.5],
       [9.9],
       [0.7]])

#########################################

The current output is当前输出为

在此处输入图像描述

The expected output is预期的输出是

在此处输入图像描述

Values bar is added following @Davide_sd.在@Davide_sd 之后添加值栏。

I'm not sure if this sovles your problem.我不确定这是否能解决您的问题。

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import numpy as np
from matplotlib.colors import Normalize
from matplotlib import cm


fig,ax = plt.subplots(1)
n=3

P=np.array([[1.9],
       [4.9],
       [6.1],
       [8.2],
       [1.8],
       [5.8],
       [9.7],
       [7.3],
       [8.9],
       [2.5],
       [9.9],
       [0.7]])

color = cm.get_cmap('Blues')
norm = Normalize(vmin=0, vmax=10)
color_list = []
for i in range(len(P)):
    color_list.append(color(P[i]/10))
print(color_list)
id = 0
for j in range(0, n):
    for k in range(n-1):
        ax.hlines(200+200*(n-j-1)+5*n, 200*(k+1)+5*n, 200*(k+2)+5*n, zorder=0, colors=color_list[id])
        id += 1

    for i in range(0, n):
        rect = mpl.patches.Rectangle((200+200*i, 200+200*j), 10*n, 10*n, linewidth=1, edgecolor='black', facecolor='black')
        ax.add_patch(rect)
        if j < n-1:
            ax.vlines(200+200*i+5*n, 200*(n-1-j)+5*n, 200*(n-j)+5*n, zorder=0, colors=color_list[id])
            id += 1

cb = fig.colorbar(cm.ScalarMappable(cmap=color, norm=norm))
cb.set_label("Values")
ax.set_xlim(left = 0, right = 220*n)
ax.set_ylim(bottom = 0, top = 220*n)
plt.show()

And the ouput is like:输出如下: 在此处输入图像描述

![在此处输入图像描述

You need to use a colormap, Normalize and ScalarMappable in order to create a colorbar.您需要使用颜色图、 NormalizeScalarMappable来创建颜色条。

Here is the procedure:这是程序:

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import matplotlib.cm as cm
from matplotlib.colors import Normalize
import numpy as np

#########################################
P=np.array([[1.9],
       [4.9],
       [6.1],
       [8.2],
       [1.8],
       [5.8],
       [9.7],
       [7.3],
       [8.9],
       [2.5],
       [9.9],
       [0.7]])

#########################################

# normalize the values. Values between 0 and 10 will be
# normalized to values from 0 and 1.
norm = Normalize(vmin=0, vmax=10)
Pnorm = norm(P)
# choose an appropriate colormap
cmap = cm.Blues

fig,ax = plt.subplots(1)
n=3
k = 0
for i in range(0,n):
    for j in range(0,n):
        rect = mpl.patches.Rectangle((200+200*i,200+200*j),10*n, 10*n, linewidth=1, edgecolor='black', facecolor='black')
        ax.add_patch(rect)
        # extract the color from the colormap
        ax.hlines(200+200*i+5*n, 200, 200*n, zorder=0, color=cmap(Pnorm[k]))
        ax.vlines(200+200*j+5*n, 200, 200*n, zorder=0, color=cmap(Pnorm[k]))
        k += 1

cb = fig.colorbar(ScalarMappable(cmap=cmap, norm=norm))
cb.set_label("Values")
ax.set_xlim(left = 0, right = 220*n)
ax.set_ylim(bottom = 0, top = 220*n)
plt.show()

There is a problem with the way you are currently plotting lines, as they are overlapping.您当前绘制线条的方式存在问题,因为它们是重叠的。 You need to fix it!你需要修复它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM