xlsxwriter:修改散点图中

时间:2017-03-26 23:18:36

标签: xlsxwriter

在Python包xlsxwriter中,是否可以以不同于另一部分的方式格式化散点图系列的一部分?例如,散点图,其中特定系列的线的某些部分是蓝色,而同一行的其他部分是红色。通过修改特定数据点,Excel本身当然是可能的。

我尝试使用'积分'许多组合中的选项没有成功。我不知道散点图中哪些选项对它有效。

更新: 这是我试图实现的一个例子。这是直接在Excel中创建的,而不是通过xlsxwriter创建的。注意线的一部分是虚线和红色,另一部分是不同的厚度。要创建它,只需选择一个数据点并使用侧栏中的选项来调整格式。

enter image description here

2 个答案:

答案 0 :(得分:1)

我已经做了一个例子,我认为可以回答你的问题。

我使用的是Python 3.5和xlsxwriter 0.9.6。

在图表1中,我根据标记是否属于特定组来更改标记的颜色。如果图表1是你正在寻找的东西,那就相当简单。

在图2中,我展示了如何使用不同颜色对连续线进行硬编码(可能有更好的方法)。

import xlsxwriter
import numpy as np
import pandas as pd

dates = pd.DataFrame({'excel_date':pd.date_range('1/1/2016', periods=12, freq='M')})
dates.excel_date = dates.excel_date - pd.datetime(1899, 12, 31)
data = np.array([11,20,25,35,40,48,44,31,25,38,49,60])
selection = np.array([4,5,6,8,11])

#Creating a list - you could hard code these lines if you prefer depending on the size of your series
diff_color_list = list()
for n in range(1, 13):
    if n in selection:
        diff_color_list.append({'fill':{'color': 'blue', 'width': 3.25}},)
    else: 
        diff_color_list.append({'fill':{'color': 'red', 'width': 3.25}},)


#Workbook Creation
workbook = xlsxwriter.Workbook("test.xlsx")
format = workbook.add_format({'num_format':'mmm-yy'})
worksheet1 = workbook.add_worksheet("testsheet")
worksheet1.write('A1', 'Date')
worksheet1.write('B1', 'Data')
worksheet1.write_column('A2', dates.excel_date, format)
worksheet1.write_column('B2', data)

chart1 = workbook.add_chart({'type': 'scatter'})

# Configure the series.
chart1.add_series({'categories': '=testsheet!$A$2:$A$13',
                   'values': '=testsheet!$B$2:$B$13',
                   'points': diff_color_list
})

chart1.set_title ({'name': 'Results'})
chart1.set_x_axis({'name': 'Date'})
chart1.set_y_axis({'name': 'Data'})
chart1.set_legend({'none': True})

# Second chart with alternating line colors
chart2 = workbook.add_chart({'type': 'scatter',
                             'subtype': 'straight'})

chart2.add_series({'categories': '=testsheet!$A$2:$A$3',
                   'values': '=testsheet!$B$2:$B$3',
                   'line':{'color': 'blue'}
})

chart2.add_series({'categories': '=testsheet!$A$3:$A$4',
                   'values': '=testsheet!$B$3:$B$4',
                   'line':{'color': 'red'}
})

chart2.add_series({'categories': '=testsheet!$A$4:$A$5',
                   'values': '=testsheet!$B$4:$B$5',
                   'line':{'color': 'blue'}
})

chart2.set_title ({'name': 'Results'})
chart2.set_x_axis({'name': 'Date'})
chart2.set_y_axis({'name': 'Data'})
chart2.set_legend({'none': True})

worksheet1.insert_chart('D6', chart1)
worksheet1.insert_chart('L6', chart2)

workbook.close()

答案 1 :(得分:1)

这个问题有点令人困惑,因为你谈到改变一部分线条的颜色,还有关于点的颜色。

我会假设您指的是更改点/标记的颜色,因为据我所知,在Excel中更改系列中线段的颜色是不可能的。

无论如何,可以使用XlsxWriter更改散点图中的标记颜色。例如:

import xlsxwriter

workbook = xlsxwriter.Workbook('chart_scatter.xlsx')
worksheet = workbook.add_worksheet()

# Add the worksheet data that the charts will refer to.
worksheet.write_column('A1', [1, 2, 3, 4, 5, 6])
worksheet.write_column('B1', [15, 40, 50, 20, 10, 50])

# Create a new scatter chart.
chart = workbook.add_chart({'type': 'scatter',
                            'subtype': 'straight_with_markers'})

# Configure the chart series. Increase the default marker size for clarity
# and configure the series points to 
chart.add_series({
    'categories': '=Sheet1!$A$1:$A$6',
    'values':     '=Sheet1!$B$1:$B$6',
    'marker': {'type': 'square',
               'size': 12},
    'points': [
        None,
        None,
        {'fill':   {'color': 'green'},                
         'border': {'color': 'black'}},
        None,
        {'fill':   {'color': 'red'},                
         'border': {'color': 'black'}},
    ],    
})

# Turn off the legend for clarity.
chart.set_legend({'none': True})

# Insert the chart into the worksheet.
worksheet.insert_chart('D2', chart)

workbook.close()

输出:

enter image description here