Matplotlib.pyplot.eventplot:将颜色与eventplot一起使用

2024-09-25 00:36:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用eventplot根据他们的标签给一些1D点上色。 以下是我想做的一个简单示例:

my_arr = [1.5, 2.4, 5.6]
my_colors = ["g", "b", "g"]
plt.eventplot(my_arr, colors=colors1)

这是我得到的stacktrace:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-82-0a7ce5b07a8c> in <module>
     12 my_arr = [1.5, 2.4, 5.6]
     13 my_colors = ["g", "b", "g"]
---> 14 plt.eventplot(my_arr, colors=colors1)

~/anaconda3/lib/python3.7/site-packages/matplotlib/pyplot.py in eventplot(positions, orientation, lineoffsets, linelengths, linewidths, colors, linestyles, data, **kwargs)
   2620         linelengths=linelengths, linewidths=linewidths, colors=colors,
   2621         linestyles=linestyles,
-> 2622         **({"data": data} if data is not None else {}), **kwargs)
   2623 
   2624 

~/anaconda3/lib/python3.7/site-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs)
   1436     def inner(ax, *args, data=None, **kwargs):
   1437         if data is None:
-> 1438             return func(ax, *map(sanitize_sequence, args), **kwargs)
   1439 
   1440         bound = new_sig.bind(ax, *args, **kwargs)

~/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_axes.py in eventplot(self, positions, orientation, lineoffsets, linelengths, linewidths, colors, linestyles, **kwargs)
   1460                              'sequences')
   1461         if len(colors) != len(positions):
-> 1462             raise ValueError('colors and positions are unequal sized '
   1463                              'sequences')
   1464         if len(linestyles) != len(positions):

ValueError: colors and positions are unequal sized sequences

我已经检查了文档和几个主题,但没有弄清楚。 我错过了什么


Tags: indatalenifmyargsaxkwargs
1条回答
网友
1楼 · 发布于 2024-09-25 00:36:24

positions变量my_arr应该是一个列表列表,其中每个内部列表包含属于同一颜色组的事件。 颜色变量my_colors应该只列出每种颜色一次,以便len(my_arr)==len(my_colors)。下面的代码应该有效:

my_arr = [[1.5, 5.6], [2.4]]
my_colors = ["g", "b"]
plt.eventplot(my_arr, colors=my_colors)

要使其看起来像1D,您可能需要将lineoffsets=0添加到eventplot的输入中:

plt.eventplot(my_arr, colors=my_colors, lineoffsets=0)

(输入错误提示:您似乎使用不同的名称来引用my_colorslater,colors1

相关问题 更多 >