Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在绘图甘特图中指定其他颜色?_Python 3.x_Plotly_Gantt Chart - Fatal编程技术网

Python 3.x 如何在绘图甘特图中指定其他颜色?

Python 3.x 如何在绘图甘特图中指定其他颜色?,python-3.x,plotly,gantt-chart,Python 3.x,Plotly,Gantt Chart,我正在使用Plotly绘制甘特图: fig=ff.create\u gantt(df,index\u col='Resource',show\u colorbar=True,group\u tasks=True) 当索引在 index\u col='Resource' 超过10将出现错误: plotly.exceptions.PlotlyError:错误。“颜色”中的颜色数不得小于组列中唯一索引值的数目 我认为这是因为Plotly的默认颜色被限制为十种不同的颜色(例如) 我想有更多的颜色(=

我正在使用Plotly绘制甘特图:

fig=ff.create\u gantt(df,index\u col='Resource',show\u colorbar=True,group\u tasks=True)
当索引在

index\u col='Resource'
超过10将出现错误:

plotly.exceptions.PlotlyError:错误。“颜色”中的颜色数不得小于组列中唯一索引值的数目

我认为这是因为Plotly的默认颜色被限制为十种不同的颜色(例如)

我想有更多的颜色(
=资源索引)
)。如何添加和定义它们

编辑: 下面是一个可用于测试的示例

from\uuuuu future\uuuuu导入打印功能
从集合导入defaultdict
从plotly.offline导入打印
将plotly.figure\u工厂作为ff导入
作为pd进口熊猫
来自《熊猫》的作者
从导入Excel文件
导入日期时间
定义甘特图测试():
“”“解决小型灵活的车间问题。”“”
#转换为日期
串行=43466.0#01.01.2019 Excel
秒=(串行-25569)*86400.0#转换为秒
date\u date=datetime.datetime.utcfromtimestamp(秒)
日期\u字符串=日期\u日期.strftime(“%Y-%m-%d%H:%m:%S”)
df=[dict(Task=“machine\u 99”,Start=0,Finish=1,Resource=“job\u 99”)]
df.clear()
起始值=0#最小值
持续时间=120#分钟
对于范围(1,12)内的i:
b_dict=dict(Task=“M”+str(i),Start=datetime.datetime.utcfromtimestamp((serial-25569+(Start_value/(60*24.0)))*86400.0)。strftime(%Y-%M-%d%H:%M:%S),Finish=datetime.datetime.utcfromtimestamp((serial-25569+(Start_value+duration)/(60*24.0))*86400.0)。strftime(%Y-%M-%d%H:%M:%S),Resource=“作业”+str(i))
df.append(b_dict)
起始值=10*i
图=ff.创建甘特图(df,index\u col='Resource',show\u colorbar=True,group\u tasks=True)
绘图(图,filename='gantt-group-tasks-together.html')
甘特图检验
如果你改变

范围(1,12)内的i的
少于

十二,

它的工作原理是:

我通过改变

fig=ff.create\u gantt(df,index\u col='Resource',show\u colorbar=True,group\u tasks=True)

fig=ff.创建甘特图(df,colors=colors,index\u col='Resource',show\u colorbar=True,group\u tasks=True)
添加
颜色
,如中所示

r=lambda:random.randint(0255)
#打印(“#%02X%02X%02X%”(r(),r(),r())
颜色=['#%02X%02X%02X%%(r(),r(),r())]
对于范围(1,15)内的i:
b_dict=dict(Task=“M”+str(i),Start=datetime.datetime.utcfromtimestamp((serial-25569+(Start_value/(60*24.0)))*86400.0)。strftime(%Y-%M-%d%H:%M:%S),Finish=datetime.datetime.utcfromtimestamp((serial-25569+(Start_value+duration)/(60*24.0))*86400.0)。strftime(%Y-%M-%d%H:%M:%S),Resource=“作业”+str(i))
df.append(b_dict)
起始值=10*i
colors.append(“#%02X%02X%02X%”(r(),r(),r())

现在为每个资源添加了随机颜色。

您还可以使用
hashlib
根据一些输入字符串(即数据框列)创建更多颜色

一个简单的例子:

导入hashlib
颜色=[]
对于键入列表字符串:
颜色=colFromStr(str(键))
颜色。附加(f“#{color}”)
图=绘图。图\u工厂。创建\u甘特图(dataToPlot,颜色=颜色)
def COLFORMSTR(输入字符串):
hashedString=hashlib.sha256(inputString.encode())
返回hashedString.hexdigest()[len(hashedString.hexdigest())-6:]

你能在你的帖子中加入你的数据框吗,这样其他人就可以很容易地重现你的问题?@glhr我已经更新了最初的帖子,并加入了一个运行示例。我使用了你的建议,但做了一些修改,使之简化了一点
r=lambda:random.randint(0255)colors=['#%02X%02X%02X%%(r(),r(),r())]用于范围(1,df.Resource.nunique()+1)中的i:colors.append('#%02X%02X%02X%%(r(),r(),r())