簡體   English   中英

python/matplotlib:創建具有變化背景的圖形 colors

[英]python/matplotlib: create graph with changing background colors

我想要一個根據某些變量改變背景 colors 的圖表,比如這個: 在此處輸入圖像描述

我的變量將確定特定 X 值的背景是灰色、紅色還是綠色。 我知道set_facecolor(color) ,但這只會改變整個背景。

您可以使用fill_between()來填充 x 軸之間的區域。 這是一個演示如何使用 x 軸執行此操作

您可以使用 matplotlib 的 colorbar 函數來創建彩色圖形。 像這樣的東西:

plt.colorbar();

我知道這是一個老問題,但如果有人像我一樣來這里尋找這個,我會發布我最終得到的解決方案。

這個 function 可用於使用狀態列表為 plot 垂直着色。

def plot_state_as_color(x_data, state_data, axis, add_labels=True):
    state_current = state_data[0]
    span_left = x_data[0]
    state_encountered = []
    for span_right, state_next in zip(x_data, state_data):
        if state_current != state_next:
            label = None
            if state_current not in state_encountered:
                state_encountered.append(state_current)
                if add_labels:
                    label = state_current

            # plot section
            color = "C{}".format(state_encountered.index(state_current))
            axis.axvspan(span_left, span_right, facecolor=color, alpha=0.5, label=label)

            # Update current state parameters
            span_left = span_right
            state_current = state_next

這是一個如何使用它的例子。

# Generate some data for the example
x_values = np.linspace(0, 10, 1000)
y_values = np.abs(np.sin(x_values))
state = [round(3*v) for v in y_values]

# figure with 4 states
plt.figure()
ax = plt.gca()
plt.plot(x_values, state, label="state")
plot_state_as_color(x_data=x_values, state_data=state, axis=ax)
plt.title("4 states")
plt.legend()

此代碼產生下圖

plot 的 4 個狀態作為顏色

state 向量也可以包含這樣的字符串

# State with string values
state_display_name = {0: "Zero",
                      1: "One",
                      2: "Two",
                      3: "Three"}
state = [state_display_name[s] for s in state]
plt.figure()
ax = plt.gca()
plot_state_as_color(x_data=x_values, state_data=state, axis=ax)
plt.title("4 states using strings as states")
plt.legend()
plt.show()

字符串作為狀態的示例

一個限制是 state 顏色 plot 不能像原始問題的圖表中那樣有漏洞。

我嘗試了您的代碼 MWJürgensen,它運行良好。 但是,它沒有為“狀態”的最后一個元素着色。 為了解決這個問題,我在 function 的末尾添加了兩行,對我來說工作正常!

def plot_state_as_color(x_data, state_data, axis, add_labels=True):
state_current = state_data[0]
span_left = x_data[0]
state_encountered = []
for span_right, state_next in zip(x_data, state_data):
    if state_current != state_next:
        label = None
        if state_current not in state_encountered:
            state_encountered.append(state_current)
            if add_labels:
                label = state_current

        # plot section
        color = "C{}".format(state_encountered.index(state_current))
        axis.axvspan(span_left, span_right, color=color, alpha=0.3, label=label)

        # Update current state parameters
        span_left = span_right
        state_current = state_next

# plot last section
label = None
if state_current not in state_encountered:
    state_encountered.append(state_current)
    label = state_current
color = "C{}".format(state_encountered.index(state_current))
ax.axvspan(span_left, span_right, color=color, alpha=set_alpha, label=label)

暫無
暫無

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

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