xlsxwriter:修改散点图中的部分线
xlsxwriter: modifying parts of a line in a scatter chart
在 Python 包 xlsxwriter 中,是否可以将散点图系列的一部分格式化为与另一部分不同的格式?例如,一个散点图,其中特定系列的线的某些部分是蓝色的,而同一线的其他部分是红色的。通过修改特定数据点,Excel 本身当然是可能的。
我尝试在许多组合中使用 'points' 选项,但均未成功。我不知道散点图中哪些选项对其有效。
更新:
这是我要实现的目标的示例。这是直接在 Excel 中创建的,而不是通过 xlsxwriter。请注意线的一部分是虚线和红色,另一部分是不同的粗细。要创建它,只需 select 一个数据点并使用边栏中的选项调整格式。
我已经举了一个我认为可以回答你问题的例子。
我正在使用 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:$A',
'values': '=testsheet!$B:$B',
'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:$A',
'values': '=testsheet!$B:$B',
'line':{'color': 'blue'}
})
chart2.add_series({'categories': '=testsheet!$A:$A',
'values': '=testsheet!$B:$B',
'line':{'color': 'red'}
})
chart2.add_series({'categories': '=testsheet!$A:$A',
'values': '=testsheet!$B:$B',
'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()
这个问题有点令人困惑,因为您谈到了更改线段的颜色以及点的颜色。
我假设您指的是更改 points/markers 的颜色,因为据我所知,在 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:$A',
'values': '=Sheet1!$B:$B',
'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()
输出:
在 Python 包 xlsxwriter 中,是否可以将散点图系列的一部分格式化为与另一部分不同的格式?例如,一个散点图,其中特定系列的线的某些部分是蓝色的,而同一线的其他部分是红色的。通过修改特定数据点,Excel 本身当然是可能的。
我尝试在许多组合中使用 'points' 选项,但均未成功。我不知道散点图中哪些选项对其有效。
更新: 这是我要实现的目标的示例。这是直接在 Excel 中创建的,而不是通过 xlsxwriter。请注意线的一部分是虚线和红色,另一部分是不同的粗细。要创建它,只需 select 一个数据点并使用边栏中的选项调整格式。
我已经举了一个我认为可以回答你问题的例子。
我正在使用 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:$A',
'values': '=testsheet!$B:$B',
'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:$A',
'values': '=testsheet!$B:$B',
'line':{'color': 'blue'}
})
chart2.add_series({'categories': '=testsheet!$A:$A',
'values': '=testsheet!$B:$B',
'line':{'color': 'red'}
})
chart2.add_series({'categories': '=testsheet!$A:$A',
'values': '=testsheet!$B:$B',
'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()
这个问题有点令人困惑,因为您谈到了更改线段的颜色以及点的颜色。
我假设您指的是更改 points/markers 的颜色,因为据我所知,在 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:$A',
'values': '=Sheet1!$B:$B',
'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()
输出: