Stu*_*nce 6 python excel data-visualization pandas xlsxwriter
我正在尝试在 XLSX 编写器中使用 3 色标进行条件格式设置,中间值为 0 中点值。我希望所有负值从红色(最低数字)缩放到黄色(当值为零时),所有正数从黄色(零时)缩放到绿色(最高)。
当我尝试以下操作时,缩放比例变得一团糟。
在 Excel 中看起来像以下内容:
我可以弄清楚如何在 XLSX 作家中做一个 3 色阶,但似乎没有一个选项(我可以看到)中点是一个数字:
worksheet.conditional_format('G2:G83', {'type': '3_color_scale',
                                     'min_color': "red",
                                     'mid_color': "yellow",
                                     'max_color': "green"})
Run Code Online (Sandbox Code Playgroud)
然后,我尝试使用一种标准将其分解,其中一种格式应用于高于零的值和低于零的值
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"})
Run Code Online (Sandbox Code Playgroud)
但这似乎也不起作用-如果有人有任何想法..请告诉我..将不胜感激。
完整的示例代码:
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],
Run Code Online (Sandbox Code Playgroud)
]
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()
Run Code Online (Sandbox Code Playgroud)
这就是我得到的:
如您所见,B列(第一列)没有绿色
C列没有红色
D 列的 0 为绿色
任何想法如何在中间为零进行 3 步缩放?
谢谢
小智 7
我知道这是一个老问题,但我刚刚遇到这个问题并想出了如何解决它。
下面是我为工作编写的实用函数的副本。最主要的是 min、mid 和 max 类型都需要是“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)
Run Code Online (Sandbox Code Playgroud)
        我可以弄清楚如何在 XLSX 作家中做一个 3 色阶,但似乎没有一个选项(我可以看到)中点是一个数字:
您可以使用min_type,mid_type和max_type参数来设置以下类型:
min        (for min_type only)
num
percent
percentile
formula
max        (for max_type only)
Run Code Online (Sandbox Code Playgroud)
请参阅条件格式选项
所以在你的情况下它应该是这样的。
worksheet1.conditional_format('D2:D12', {'type': '3_color_scale',
                                         'min_color': "red",
                                         'mid_color': "yellow",
                                         'max_color': "green",
                                         'mid_type': "num"})
Run Code Online (Sandbox Code Playgroud)
但是,我不确定这是否会解决您的整体问题。也许将其添加到您的示例中,如果它不起作用,则打开第二个问题。
您必须弄清楚的一件事是如何首先在 Excel 中执行您想要的操作。之后通常更容易弄清楚 XlsxWriter 需要什么。