Кривая Pyplot с условными цветами [дубликат]

Это просто! Вы создаете новый таймер.

Timer timer = new Timer();

Затем вы увеличиваете задачу таймера

class UpdateBallTask extends TimerTask {
   Ball myBall;

   public void run() {
       //calculate the new position of myBall
   }
}

, а затем добавляете новую задачу в таймер с некоторым интервалом обновления

final int FPS = 40;
TimerTask updateBall = new UpdateBallTask();
timer.scheduleAtFixedRate(updateBall, 0, 1000/FPS);

Отказ от ответственности: это не идеальное решение. Это решение, использующее класс Timer (по запросу OP). В Android SDK рекомендуется использовать класс Handler (есть пример в принятом ответе).

11
задан wuwucat 21 June 2013 в 18:04
поделиться

4 ответа

Присвоить выбор цвета @JoeKington,

import numpy as np
import matplotlib.pyplot as plt

def uniqueish_color(n):
    """There're better ways to generate unique colors, but this isn't awful."""
    return plt.cm.gist_ncar(np.random.random(n))

plt.scatter(latt, lont, c=uniqueish_color(len(latt)))

Вы можете сделать это с помощью scatter.

20
ответ дан tacaswell 22 August 2018 в 19:22
поделиться
  • 1
    Я думаю, что OP хочет маркеров точки, а не отрезков. – tacaswell 21 June 2013 в 18:55
  • 2
    Ах, я предположил, что ему нужны строки из «теперь я хочу построить это как строку», но при повторном чтении вы, вероятно, правы. – Joe Kington 21 June 2013 в 18:56
21
ответ дан tacaswell 5 November 2018 в 16:36
поделиться

Скопировано из в этом примере :

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib.colors import ListedColormap, BoundaryNorm

x = np.linspace(0, 3 * np.pi, 500)
y = np.sin(x)
z = np.cos(0.5 * (x[:-1] + x[1:]))  # first derivative

# Create a colormap for red, green and blue and a norm to color
# f' < -0.5 red, f' > 0.5 blue, and the rest green
cmap = ListedColormap(['r', 'g', 'b'])
norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N)

# Create a set of line segments so that we can color them individually
# This creates the points as a N x 1 x 2 array so that we can stack points
# together easily to get the segments. The segments array for line collection
# needs to be numlines x points per line x 2 (x and y)
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)

# Create the line collection object, setting the colormapping parameters.
# Have to set the actual values used for colormapping separately.
lc = LineCollection(segments, cmap=cmap, norm=norm)
lc.set_array(z)
lc.set_linewidth(3)

fig1 = plt.figure()
plt.gca().add_collection(lc)
plt.xlim(x.min(), x.max())
plt.ylim(-1.1, 1.1)

plt.show()
3
ответ дан ali_m 22 August 2018 в 19:22
поделиться

См. ответ здесь , чтобы генерировать «периоды», а затем использовать функцию matplotlib разброс как упомянутый @tcaswell. Используя функцию plot.hold , вы можете отображать каждый период, цвета будут увеличиваться автоматически.

2
ответ дан Community 22 August 2018 в 19:22
поделиться
Другие вопросы по тегам:

Похожие вопросы: