2

I have two figures with 3 subplots in each of them. In each subplot, there are 20 different curves, which represents 20 steps, by using a for loop. How can I make the color of the curves gradually fade? Like in the code I have below, the top subplot (311) has 20 blue curves...how can I make the 1st step be dark blue and have the blue gradually fade until the last step be a light blue? Also, how do I make the two figures pop up on screen at once? Right now, I have to manually close the first figure in order for the second figure to pop up.

from pylab import *
for raw_step in raw:
    raw_step = zip(*raw_step)
    Raw_Vertex, Raw_KI, Raw_KII, Raw_KIII = raw_step[0], raw_step[1], raw_step[2], raw_step[3]

    subplot(311)
    plot(Raw_Vertex, Raw_KI, 'bo-')
    grid(True)
    title('Raw SIFs')
    ylabel('K_I')

    subplot(312)
    plot(Raw_Vertex, Raw_KII, 'go-')
    grid(True)
    ylabel('K_II')

    subplot(313)
    plot(Raw_Vertex, Raw_KIII, 'ro-')
    grid(True)
    xlabel('Vertex')
    ylabel('K_III')

show()


for mls_step in mls:
    mls_step = zip(*mls_step)
    MLS_Vertex, MLS_KI, MLS_KII, MLS_KIII = mls_step[0], mls_step[1], mls_step[2], mls_step[3]

    subplot(311)
    plot(MLS_Vertex, MLS_KI, 'bo-')
    grid(True)
    title('MLS SIFs')
    ylabel('K_I')

    subplot(312)
    plot(MLS_Vertex, MLS_KII, 'go-')
    grid(True)
    ylabel('K_II')

    subplot(313)
    plot(MLS_Vertex, MLS_KIII, 'ro-')
    grid(True)
    xlabel('Vertex')
    ylabel('K_III')

show()
4

2 に答える 2

9

青の明るい色合いを得るには、次を使用します

blues = plt.get_cmap('Blues')  # this returns a colormap
color = blues(1 - float(i)/(len(raw)-1)) # blues(x) returns a color for each x between 0.0 and 1.0

サブプロットを並べて配置するには、 を使用fig.add_subplots(row, columns, n)して 6 つの軸を定義します。

fig = plt.figure()
ax[1] = fig.add_subplot(3, 2, 1) # 3x2 grid, 1st plot
...
ax[6] = fig.add_subplot(3, 2, 6) # 3x2 grid, 6th plot

import matplotlib.pyplot as plt
import numpy as np

raw = range(20)
mls = range(20)
ax = {}
blues = plt.get_cmap('Blues')
reds = plt.get_cmap('Reds')
greens = plt.get_cmap('Greens')

fig = plt.figure()

ax[1] = fig.add_subplot(3, 2, 1)
ax[1].set_title('Raw SIFs')
ax[1].grid(True)
ax[1].set_ylabel('K_I')

ax[3] = fig.add_subplot(3, 2, 3)
ax[3].grid(True)
ax[3].set_ylabel('K_II')

ax[5] = fig.add_subplot(3, 2, 5)
ax[5].grid(True)
ax[5].set_xlabel('Vertex')
ax[5].set_ylabel('K_III')

ax[2] = fig.add_subplot(3, 2, 2)
ax[2].set_title('MLS SIFs')
ax[2].grid(True)
ax[2].set_ylabel('K_I')

ax[4] = fig.add_subplot(3, 2, 4)
ax[4].grid(True)
ax[4].set_ylabel('K_II')

ax[6] = fig.add_subplot(3, 2, 6)
ax[6].grid(True)
ax[6].set_xlabel('Vertex')
ax[6].set_ylabel('K_III')

for i, raw_step in enumerate(raw):
    Raw_Vertex = np.arange(10)
    Raw_KI = Raw_Vertex*(i+1)
    Raw_KII = Raw_Vertex*(i+1)
    Raw_KIII = Raw_Vertex*(i+1)
    ax[1].plot(Raw_Vertex, Raw_KI, 'o-', color = blues(1 - float(i)/(len(raw)-1)))
    ax[3].plot(Raw_Vertex, Raw_KII, 'o-', color = greens(1 - float(i)/(len(raw)-1)))
    ax[5].plot(Raw_Vertex, Raw_KIII, 'o-', color = reds(1 - float(i)/(len(raw)-1)))

for i, mls_step in enumerate(mls):
    MLS_Vertex = np.arange(10)
    MLS_KI = MLS_Vertex**2*(i+1)
    MLS_KII = MLS_Vertex**2*(i+1)
    MLS_KIII = Raw_Vertex**2*(i+1)

    ax[2].plot(MLS_Vertex, MLS_KI, 'o-', color = blues(1 - float(i)/(len(mls)-1)))
    ax[4].plot(MLS_Vertex, MLS_KII, 'o-', color = greens(1 - float(i)/(len(mls)-1)))
    ax[6].plot(MLS_Vertex, MLS_KIII, 'o-', color = reds(1 - float(i)/(len(mls)-1)))

plt.show()

ここに画像の説明を入力

于 2012-08-05T21:53:40.067 に答える
3

色の選択にもう少し柔軟性が必要な場合は、colorsys;を使用することをお勧めします。

異なるカラー マップ間で変換するための非常に便利な関数がいくつかあります。 http://en.wikipedia.org/wiki/HSL_and_HSV、これにより柔軟性が大幅に向上します。

http://docs.python.org/library/colorsys.html

次のように使用できます。

ax[1].plot(Raw_Vertex, Raw_KI, 'o-', color =colorsys.hsv_to_rgb(0,1-i/float(curves),1))

明るさ、暗さ、色を任意の場所に、より直感的な方法で簡単に変更できます。 ここに画像の説明を入力

于 2012-10-16T10:00:01.710 に答える