OiO.lk Blog python How to have Mixed Linestyles and Colors in Matplotlib
python

How to have Mixed Linestyles and Colors in Matplotlib


I would like to create a line chart with two different line styles in a single chart while customizing the color in each line on a pandas dataframe. For example, if the below customer spends over 100, they are flagged as a "Premium" customer. Otherwise, they are a "Standard" customer.

My goal is to plot how much the customer is spending a week, using a solid line when Premium. I am stuck on how to color code the customer’s line and using multiple line styles. Is this possible?

”’

import pandas as pd
data = {'Date': ['9/1/2024', '9/8/2024', '9/15/2024', '9/22/2024', '9/27/2024', '10/3/2024', '9/1/2024', '9/8/2024', '9/15/2024', '9/22/2024', '9/27/2024', '10/3/2024', '9/1/2024', '9/8/2024', '9/15/2024', '9/22/2024', '9/27/2024', '10/3/2024'],
        'Customer': ['Customer A', 'Customer A', 'Customer A', 'Customer A', 'Customer A', 'Customer A', 'Customer B', 'Customer B', 'Customer B', 'Customer B', 'Customer B', 'Customer B', 'Customer C', 'Customer C', 'Customer C', 'Customer C', 'Customer C', 'Customer C'],
        'Spend': [100, 200, 10, 150, 180, 200, 100, 200, 10, 15, 200, 200, 100, 200, 10, 10, 300, 150],
        'Color': ['Red', 'Red', 'Red', 'Red', 'Red', 'Red', 'Orange', 'Orange', 'Orange', 'Orange', 'Orange', 'Orange', 'Yellow', 'Yellow', 'Yellow', 'Yellow', 'Yellow', 'Yellow']}
df['PurchaseType'] = np.where(df['Spend']>=100, 'A', 'B')

”’

I’ve tried masking the data similar to this post Replacing part of a plot with a dotted line but am stuck at the plotting.



You need to sign in to view this answers

Exit mobile version