Hooloovoo
Hooloovoo

Reputation: 875

Python matplotlib plotting an array with different colors

I have a file that combines data sets from different sources but preserves the common format. I want to plot these using different colors to indicate the different sources of the data sets. For example, a few lines in the data file look like this:

# Source measurement1 measurement2 error color
SiteA  543.2 12.3 0.01 blue
SiteB  545.6 12.5 0.02 red
SiteA  545.9 12.9 0.01 blue
SiteC  549.1 13.2 0.01 orange
SiteB  550.4 13.3 0.02 red
...

At the moment I do a for loop and plot each point:

for point in data:
   plt.errorbar(measurement1,measurement2,yerr=error, marker='.', ecolor='k', fmt=color, linestyle='.')

This plots each point individually but can take a very long time for large data arrays.

Can anyone suggest a faster way of doing it?

Upvotes: 1

Views: 1724

Answers (1)

wim
wim

Reputation: 362478

If you don't have too many colours, you should be able to speed things up by plotting in groups of colours, i.e. calling pyplot.errorbar() once for each colour. Use list comprehension to group the data into colour groups, and provide lists or arrays instead of scalars for measurement1, measurement2 etc.

Upvotes: 2

Related Questions