Pandas: 使用plotly作为后端时如何选择绘图元素颜色?

Pandas: How to choose plot element colors when using plotly as a backend?

我正在试验 Plotly as a pandas plot backend. I successfuly configured it following these instructions,但我在配置图表元素颜色时遇到问题。

这是我想做的。它与 matplotlib 作为后端一起工作:

df.plot(kind='area', 
        color=['orange', 'lightslategray'],
        backend='matplotlib' )

但是当我尝试使用 ploty 时,它失败并出现一个奇怪的错误:

df.plot(kind='area', 
        color=['orange', 'lightslategray'], 
        backend='plotly' )

错误:

ValueError: All arguments should have the same length. The length of argument `color` is 2, whereas the length of  previously-processed arguments ['Dia', 'Alfa', 'Beta'] is 12

如果我不尝试设置任何颜色,情节就有效:

df.plot(kind='area', backend='plotly' )

根据您的绘图后端,某些参数的含义似乎不同。 color 就是其中之一。使用 plotly 时,它会尝试选择将用作颜色的维度。正确的方法是使用color_discrete_map参数:

df.plot(kind='area', 
        color_discrete_map={
            'Alfa': 'orange', 
            'Beta': 'lightslategray'}, 
        backend='plotly' )

很遗憾,无法互换后端引擎。