簡體   English   中英

繪制具有不同顏色的數字序列

[英]plot a sequence of numbers with different colors

我有一個 0 和 1 的隨機列表,長度 > 300。我想繪制列表,其中 1 為綠色,0 為紅色,如下圖所示。 在 matplotlib 中執行此操作的最佳方法是什么?

在此處輸入圖片說明

您可以使用matplotlib 表

import matplotlib.pyplot as plt

data = [0,1,0,1,1,0]  # Setup data list
fig, ax = plt.subplots(figsize=(len(data)*0.5, 0.5))  # Setup figure
ax.axis("off")  # Just want table, no actual plot

# Create table, with our data array as the single row, and consuming the whole figure space
t = ax.table(cellText=[data], loc="center", cellLoc="center", bbox=[0,0,1,1])

# Iterate over cells to colour them based on value
for idx, cell in t.get_celld().items():
    if data[idx[1]] == 1:
        c = 'g'
    else:
        c = 'r'
    cell.set_edgecolor(c)
    cell.set_facecolor(c)

fig.show()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM