在 xlsxwriter 图表中管理颜色
managing color in xlsxwriter chart
Excel 2016 / Python 2.7.6 / XlsxWriter 1.1.1
我正在将代码写入 select 个文件,将它们编译成一个 .xlsx 文件,然后将它们绘制在各自工作表上的 2 个图表上。理想情况下,我想强制同一选项卡中的系列使用相同的颜色。不幸的是,xlsxwriter 无法识别 Excel 2016 中的颜色名称。
我更愿意使用 Excel 默认选择的颜色顺序。其中一张图表很好(我没有指定颜色,它只有一个 x 和 y 轴,每个选项卡一个系列)。另一个则不然。我正在将一个系列绘制成一条针对左 Y 轴的线,我正在针对右 Y 轴绘制一个系列,我想匹配颜色,我正在绘制第三个系列也针对右 Y 轴,我想只为此使用标记。选项卡的数量没有限制,但通常会有 5 个。
默认颜色为蓝色、红色、橄榄绿、紫色、浅绿色和橙色(我没有超出这些颜色)。我尝试创建这些颜色的列表,但它无法识别橄榄绿或浅绿色。把它们改成绿色和青色太苛刻了。
有没有办法允许 xlsxwriter select 颜色,然后获取该系列的颜色并将其应用到下一个系列?最好找到每种颜色的十六进制代码并改用它吗?
colors = ['blue', 'red', 'Olive Green', 'purple', 'Aqua', 'orange', 'lime', 'magenta', 'navy', 'pink', 'cyan',
'silver', 'brown'] #named colors to cycle through
series_to_delete = [] #for legend simplicity
series = len(files)
j = 0
for df, tab in zip(dfs, tabs):
if j > len(colors): #if you run out of colors, go back to the beginning
j = 0
color = colors[j] #choose a color
df.to_excel(writer, tab) #write the df to a tab
x_categories = "='" + tab + "'!$a:$a00" #for the first plot
y_values = "='" + tab + "'!$c:$c00" #y values used in both plots
x2_categories = "='" + tab + "'!$b:$b00" #second plot, x axis
y2_values = "='" + tab + "'!$d:$d00" #second plot, second y axis
x3_categories = "='" + tab + "'!$e:$e00" #these are markers
y3_values = "='" + tab + "'!$f:$f00" #this is the 3rd series on plot 2
chart.add_series({'name': tab, 'categories': x_categories, 'values': y_values}) #plot 1, note no named color
chart2.add_series({'name': tab, 'categories': x2_categories, 'values': y_values, 'line': {'color': color}}) #plot 2, against 1st y axis
chart2.add_series({'name': tab, 'categories': x2_categories, 'values': y2_values, 'y2_axis': 1,
'line': {'color': color, 'transparency': 50}}) #plot 2, against 2nd y axis
chart2.add_series({'categories': x3_categories, 'values': y3_values, 'line': {'none': True},
'marker': {'type': 'automatic'}, 'y2_axis': 1,
'data_labels': {'value': True, 'category': True, 'separator': "\n"}}) #plot 2, against 2nd y axis
j += 1
series_to_delete.append(series) #this is so the legend isn't cluttered
series_to_delete.append(series + 1) #with each series named 3X
series += 2
chart2.set_legend({'delete_series': series_to_delete})
Is it best to find the Hex code for each color and use that instead?
是的。 named colors in XlsxWriter 的数量有限,它们的存在只是为了向后兼容的原因和使示例更清晰一些。
如果您想要自己的调色板,那么您可以使用 HTML 颜色选择器 (such as this one)。
请注意,您的 $a:$a00
等范围是错误的。 Excel 使用大写列字母。更好的方法是避免尝试手动构建这些类型的范围,而是使用类别和值的列表语法,如下所示:
chart.add_series({'name': tab,
'categories': [tab, 1, 0, 4999, 0],
'values': [tab, 1, 2, 4999, 2]})
有关此列表语法的说明,请参阅 add_series() 上的文档。
Excel 2016 / Python 2.7.6 / XlsxWriter 1.1.1
我正在将代码写入 select 个文件,将它们编译成一个 .xlsx 文件,然后将它们绘制在各自工作表上的 2 个图表上。理想情况下,我想强制同一选项卡中的系列使用相同的颜色。不幸的是,xlsxwriter 无法识别 Excel 2016 中的颜色名称。
我更愿意使用 Excel 默认选择的颜色顺序。其中一张图表很好(我没有指定颜色,它只有一个 x 和 y 轴,每个选项卡一个系列)。另一个则不然。我正在将一个系列绘制成一条针对左 Y 轴的线,我正在针对右 Y 轴绘制一个系列,我想匹配颜色,我正在绘制第三个系列也针对右 Y 轴,我想只为此使用标记。选项卡的数量没有限制,但通常会有 5 个。
默认颜色为蓝色、红色、橄榄绿、紫色、浅绿色和橙色(我没有超出这些颜色)。我尝试创建这些颜色的列表,但它无法识别橄榄绿或浅绿色。把它们改成绿色和青色太苛刻了。
有没有办法允许 xlsxwriter select 颜色,然后获取该系列的颜色并将其应用到下一个系列?最好找到每种颜色的十六进制代码并改用它吗?
colors = ['blue', 'red', 'Olive Green', 'purple', 'Aqua', 'orange', 'lime', 'magenta', 'navy', 'pink', 'cyan',
'silver', 'brown'] #named colors to cycle through
series_to_delete = [] #for legend simplicity
series = len(files)
j = 0
for df, tab in zip(dfs, tabs):
if j > len(colors): #if you run out of colors, go back to the beginning
j = 0
color = colors[j] #choose a color
df.to_excel(writer, tab) #write the df to a tab
x_categories = "='" + tab + "'!$a:$a00" #for the first plot
y_values = "='" + tab + "'!$c:$c00" #y values used in both plots
x2_categories = "='" + tab + "'!$b:$b00" #second plot, x axis
y2_values = "='" + tab + "'!$d:$d00" #second plot, second y axis
x3_categories = "='" + tab + "'!$e:$e00" #these are markers
y3_values = "='" + tab + "'!$f:$f00" #this is the 3rd series on plot 2
chart.add_series({'name': tab, 'categories': x_categories, 'values': y_values}) #plot 1, note no named color
chart2.add_series({'name': tab, 'categories': x2_categories, 'values': y_values, 'line': {'color': color}}) #plot 2, against 1st y axis
chart2.add_series({'name': tab, 'categories': x2_categories, 'values': y2_values, 'y2_axis': 1,
'line': {'color': color, 'transparency': 50}}) #plot 2, against 2nd y axis
chart2.add_series({'categories': x3_categories, 'values': y3_values, 'line': {'none': True},
'marker': {'type': 'automatic'}, 'y2_axis': 1,
'data_labels': {'value': True, 'category': True, 'separator': "\n"}}) #plot 2, against 2nd y axis
j += 1
series_to_delete.append(series) #this is so the legend isn't cluttered
series_to_delete.append(series + 1) #with each series named 3X
series += 2
chart2.set_legend({'delete_series': series_to_delete})
Is it best to find the Hex code for each color and use that instead?
是的。 named colors in XlsxWriter 的数量有限,它们的存在只是为了向后兼容的原因和使示例更清晰一些。
如果您想要自己的调色板,那么您可以使用 HTML 颜色选择器 (such as this one)。
请注意,您的 $a:$a00
等范围是错误的。 Excel 使用大写列字母。更好的方法是避免尝试手动构建这些类型的范围,而是使用类别和值的列表语法,如下所示:
chart.add_series({'name': tab,
'categories': [tab, 1, 0, 4999, 0],
'values': [tab, 1, 2, 4999, 2]})
有关此列表语法的说明,请参阅 add_series() 上的文档。