XLSX Writer Python- 3色标,数字为中点

时间:2017-04-12 23:31:00

标签: python excel pandas data-visualization xlsxwriter

我正在尝试使用3色标度在XLSX编写器中进行条件格式化,中间值为0。我希望所有负值从红色(最低数字)到黄色(当值为零时)和所有正数从黄色(从零)到绿色(最高)。

当我尝试以下操作时,缩放会全部搞砸..

在Excel中看起来像以下内容:

enter image description here

我可以弄清楚如何在XLSX编写器中进行3色标度,但似乎没有一个选项(我可以看到)中点是一个数字:

worksheet.conditional_format('G2:G83', {'type': '3_color_scale',
                                     'min_color': "red",
                                     'mid_color': "yellow",
                                     'max_color': "green"})

然后我尝试用一​​个标准将其分解,其中一种格式应用于零以上和零以下的值

worksheet.conditional_format('G2:G83', {'type': '2_color_scale',
                                    'criteria': '<',
                                    'value': 0,
                                    'min_color': "red",
                                    'max_color': "yellow"})


worksheet.conditional_format('G2:G83', {'type': '2_color_scale',
                                    'criteria': '>',
                                    'value': 0,
                                     'min_color': "yellow",
                                     'max_color': "green"})

但这似乎也不起作用 - 如果有人有任何想法......请让我知道..真的很感激。

完整的示例代码:

import xlsxwriter

workbook = xlsxwriter.Workbook('conditional_format.xlsx')
worksheet1 = workbook.add_worksheet()


# Add a format. Light red fill with dark red text.
format1 = workbook.add_format({'bg_color': '#FFC7CE',
                           'font_color': '#9C0006'})

# Add a format. Green fill with dark green text.
format2 = workbook.add_format({'bg_color': '#C6EFCE',
                           'font_color': '#006100'})

# Some sample data to run the conditional formatting against.
data = [
[34, 72, -38, 30, 75, 48, 75, 66, 84, 86],
[-6, -24, 1, -84, 54, 62, 60, 3, 26, 59],
[-28, 0, 0, 13, -85, 93, 93, 22, 5, 14],
[27, -71, -40, 17, 18, 79, 90, 93, 29, 47],
[0, 25, -33, -23, 0, 1, 59, 79, 47, 36],
[-24, 100, 20, 88, 29, 33, 38, 54, 54, 88],
[6, -57, -88, 0, 10, 26, 37, 7, 41, 48],
[-52, 78, 1, -96, 26, -45, 47, 33, 96, 36],
[60, -54, -81, 66, 81, 90, 80, 93, 12, 55],
[-70, 5, 46, 14, 71, -19, 66, 36, 41, 21],

for row, row_data in enumerate(data):
    worksheet1.write_row(row + 2, 1, row_data)


worksheet1.conditional_format('B2:B12', {'type': '2_color_scale',
                                    'criteria': '<',
                                    'value': 0,
                                    'min_color': "red",
                                    'max_color': "yellow"})


worksheet1.conditional_format('C2:C12', {'type': '2_color_scale',
                                    'criteria': '>',
                                    'value': 0,
                                     'min_color': "yellow",
                                     'max_color': "green"})

worksheet1.conditional_format('C2:C12', {'type': '2_color_scale',
                                    'criteria': '<',
                                    'value': 0,
                                     'min_color': "red",
                                     'max_color': "yellow"})


worksheet1.conditional_format('D2:D12', {'type': '3_color_scale',
                                     'min_color': "red",
                                     'mid_color': "yellow",
                                     'max_color': "green"})







workbook.close()    
writer.save()

这就是我得到的:

enter image description here

如您所见,B列(第一列)没有绿色

C列没有红色

D列为0为绿色

任何想法如何在中间进行3步缩放?

由于

2 个答案:

答案 0 :(得分:2)

我知道这是一个古老的问题,但是我碰到了这个问题并想出了解决方法。

下面是我为我的工作编写的实用程序功能的副本。最主要的是,最小,中间和最大类型ALL必须为'num',并且需要为这些点指定值。

如果仅将中间类型设置为'num',将值设置为0,则3色标仍将使用最小值和最大值作为终点。这意味着,如果列的内容全部在枢轴点的一侧,则着色实际上将忽略枢轴。

from xlsxwriter.utility import xl_col_to_name as index_to_col

MIN_MIN_FORMAT_VALUE = -500
MAX_MAX_FORMAT_VALUE = 500

def conditional_color_column(
        worksheet, df, column_name, min_format_value=None, pivot_value=0, max_format_value=None):
    """
    Do a 3 color conditional format on the column.

    The default behavior for the min and max values is to take the min and max values of each column, unless said value
    is greater than or less than the pivot value respectively at which point the values MIN_MIN_FORMAT_VALUE and
    MAX_MAX_FORMAT_VALUE are used. Also, if the min and max vales are less than or greater than respectively of
    MIN_MIN_FORMAT_VALUE and MAX_MAX_FORMAT_VALUE then the latter will be used

    :param worksheet: The worksheet on which to do the conditional formatting
    :param df: The DataFrame that was used to create the worksheet
    :param column_name: The column to format
    :param min_format_value: The value below which all cells will have the same red color
    :param pivot_value: The pivot point, values less than this number will gradient to red, values greater will gradient to green
    :param max_format_value: The value above which all cells will have the same green color
    :return: Nothing
    """
    column = df[column_name]
    min_value = min(column)
    max_value = max(column)

    last_column = len(df.index)+1
    column_index = df.columns.get_loc(column_name)
    excel_column = index_to_col(column_index)
    column_to_format = f'{excel_column}2:{excel_column}{last_column}'

    if min_format_value is None:
        min_format_value = max(min_value, MIN_MIN_FORMAT_VALUE)\
            if min_value < pivot_value else MIN_MIN_FORMAT_VALUE

    if max_format_value is None:
        max_format_value = min(max_value, MAX_MAX_FORMAT_VALUE)\
            if max_value > pivot_value else MAX_MAX_FORMAT_VALUE

    color_format = {
        'type': '3_color_scale',
        'min_type': 'num',
        'min_value': min_format_value,
        'mid_type': 'num',
        'mid_value': pivot_value,
        'max_type': 'num',
        'max_value': max_format_value
    }
    worksheet.conditional_format(column_to_format, color_format)

答案 1 :(得分:1)

  

我可以弄清楚如何在XLSX编写器中进行3色标度,但似乎没有一个选项(我可以看到)中点是一个数字:

您可以使用min_typemid_typemax_type参数设置以下类型:

min        (for min_type only)
num
percent
percentile
formula
max        (for max_type only)

请参阅Conditional Format Options

所以在你的情况下它应该是类似的东西。

worksheet1.conditional_format('D2:D12', {'type': '3_color_scale',
                                         'min_color': "red",
                                         'mid_color': "yellow",
                                         'max_color': "green",
                                         'mid_type': "num"})

但是,我不确定这是否能解决您的整体问题。也许将它添加到您的示例中,如果它不起作用,则打开第二个问题。

您必须弄清楚的一件事是如何在Excel中首先执行您想要的操作。之后,通常更容易弄清楚XlsxWriter中需要什么。