2017-03-26 255 views
3

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

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

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

enter image description here

回答

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

感谢您的回答。我的意思是你在#2中创建的。然而,由于我可能有10个或更多的不同部分,并且每个部分都需要一个单独的系列,所以您的方法可能会让我感到混乱。例如,这个传说将无法理解。 – Tinkerer

+1

也许不会显示传说将是最好的。我认为唯一能做到你想做的就是对每一个add_series进行硬编码。就像你说的那样,如果你有很多不同的部分,会变得混乱。请注意,模块作者(@jmcnamara)在他的回答中表示,在一个系列中没有办法做到这一点,我想说这是一个很好的权威。 – patrickjlong1

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

+0

感谢您的评论;但是我对标记不感兴趣(我的图不使用它们),但是在标记本身中。用一个例子来看问题的更新。 – Tinkerer

+0

感谢您的澄清。它看起来像我错了,这*是由Excel支持。但是,由于我之前并不知道它在XlsxWriter中不可用(我是作者)。如果你打开一个功能请求,我可以添加它。 – jmcnamara

+0

谢谢,我一定会添加一个功能请求。 – Tinkerer