Alternating line colors

1,996 views
Skip to first unread message

Luke M

unread,
Jun 23, 2014, 10:42:22 AM6/23/14
to pyqt...@googlegroups.com
I frequently have multiple lines on one plot so I always have to change colors. Several of my applications have a variable number of lines so I needed a smart way to alternate colors. I'm not sure if there is already a solution in place for this but I didn't find anything so this is how I'm doing it. I don't know if it would be possible to incorporate something like this into PlotItem.plot so that it is handled automatically, but figured I'd share in case anyone was interested. The example rotates through 7 colors, then starts over.
 
import pyqtgraph as pg
import numpy as np
app = pg.QtGui.QApplication([])
LINECOLORS = ['r', 'g', 'b', 'c', 'm', 'y', 'w']

plt = pg.PlotWidget()
for k in range(9):
    colors = [di.color for di in plt.plotItem.dataItems]
    color_count = [colors.count(c) for c in LINECOLORS]
    color = LINECOLORS[color_count.index(min(color_count))]
    pen = pg.mkPen(color)
    x = np.arange(0, 10, 2)
    y = x + k
    line = plt.plot(x, y, pen=pen)
    line.color = color
plt.show()


Luke Campagnola

unread,
Jun 23, 2014, 11:20:21 AM6/23/14
to pyqt...@googlegroups.com
On Mon, Jun 23, 2014 at 10:42 AM, Luke M <lcj...@gmail.com> wrote:
I frequently have multiple lines on one plot so I always have to change colors. Several of my applications have a variable number of lines so I needed a smart way to alternate colors. I'm not sure if there is already a solution in place for this but I didn't find anything so this is how I'm doing it. I don't know if it would be possible to incorporate something like this into PlotItem.plot so that it is handled automatically, but figured I'd share in case anyone was interested. The example rotates through 7 colors, then starts over.

You can access the same functionality by specifying colors as a tuple of two ints: `pg.plot(data, pen=(3,7))`

However, your solution is unique in that it actually checks the colors present in the plot when deciding what color to use next, whereas I usually just increment a counter.

Luke M

unread,
Jun 23, 2014, 4:28:22 PM6/23/14
to pyqt...@googlegroups.com

On Monday, June 23, 2014 11:20:21 AM UTC-4, Luke Campagnola wrote:

 
You can access the same functionality by specifying colors as a tuple of two ints: `pg.plot(data, pen=(3,7))`

However, your solution is unique in that it actually checks the colors present in the plot when deciding what color to use next, whereas I usually just increment a counter.
 
Ah yes, I had forgotten about that. It's been a while since I did this and was just working with it again recently and thought I'd post it. I think initially I had used your technique but in my application the user can pick from a list of channels to plot and they can turn those channels on and off, so frequently I would get two lines with the same color.
 
Also, now that I'm thinking about it, I'm partially color blind and I couldn't find a set of hues that were distinct enough. In my solution, I can set blue and red as the first two colors and they are very different to my eye. But that's more of a personal preference thing.
 
So I guess they are similar solutions with a subtle difference. Thanks for the reply!
Reply all
Reply to author
Forward
0 new messages