如何从循环中重用颜色?

时间:2019-07-17 18:00:26

标签: python matplotlib

我在循环中有两个plot语句。我希望它们在一个颜色循环中都使用相同的颜色,并且只增加一次颜色循环。目的是使每条曲线及其回归线具有相同的颜色,并且仅在整个循环中步进一次。我该怎么办?

    colormap = plt.cm.hsv
    ax.set_prop_cycle('color', [colormap(i) for i in np.linspace(0.0, 0.9, self.numplots)])
    # plot the lines
    j = 0
    start = 40
    incrmnt = 10
    for line, rank in sortedSymbols:
        series = getattr(self, line)["CLOSE"]
        dates = pd.to_datetime(getattr(self, line)["DATE"]).dt.date
        name = self.symToNames[line]
        ax.plot(dates.iloc[-250:], series.iloc[-250:]/series.iloc[-250] * (start+j), label = name)
        y = series.iloc[-1]/series.iloc[-250] * (start+j)
        ax.annotate(name, xy=(1,y), xytext=(6,0), xycoords = ax.get_yaxis_transform(),
            textcoords="offset points", size=10, va="center")
        slope, intercept = np.polyfit(dates.iloc[-50:].index,
            series.iloc[-50:]/series.iloc[-250] * (start+j),1)
        ax.plot(dates.iloc[-50:], slope*dates.iloc[-50:].index+intercept)#, color = "gray")
        j += incrmnt

Color cycle example

0 个答案:

没有答案