Python matplotlib 用不同颜色绘制数组
我有一个文件,它组合了来自不同来源的数据集,但保留了通用格式。 我想使用不同的颜色来绘制这些数据以指示数据集的不同来源。 例如,数据文件中的几行如下所示:
# 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
...
目前,我执行一个 for 循环并绘制每个点:
for point in data:
plt.errorbar(measurement1,measurement2,yerr=error, marker='.', ecolor='k', fmt=color, linestyle='.')
这会单独绘制每个点,但对于大型数据数组可能需要很长时间。
谁能建议一种更快的方法?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您没有太多颜色,您应该能够通过按颜色组进行绘制来加快速度,即为每种颜色调用一次 pyplot.errorbar() 。使用列表理解将数据分组为颜色组,并为测量1、测量2等提供列表或数组而不是标量。
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.