Xlsxwriter: change parts of a line in a scatterplot

in the xlsxwriter Python package, is it possible to format part of a scatterplot series differently than another? for example, a scatter plot where some portions of a line in a particular series are blue and other portions of the same line are red. this is certainly possible in Excel itself by changing specific data points.

I have tried using the "points" option in many combinations without success. I don't know what options are valid for it in the scatterplot.

UPDATE: Here is an example of what I am trying to achieve. This was created directly in Excel, not through xlsxwriter. Note that one part of the line is dashed and red, and the other is the other. To create one, simply select a data point and use the options in the sidebar to customize the formatting.

enter image description here

+3


source to share


2 answers


I have provided an example which I think answers your question.

I am using Python 3.5 and xlsxwriter 0.9.6.

In Chart 1, I changed the color of the markers based on whether they were in a specific group. It's pretty straightforward if Chart 1 is what you are looking for.



In Diagram 2, I'll show you how to hard-code a continuous line with different colors (perhaps the best way to do this).

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


source


The question is a little confusing because you are talking about changing the color of parts of a line as well as dots.

I am assuming you mean changing the color of the points / markers, as to my knowledge changing the color of line segments in a series is not possible in Excel.

In any case, you can change the colors of the markers in the scatterplot using the XlsxWriter. For example:



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()

      

Output:

enter image description here

+1


source







All Articles