Python Forum
matplotlib Plotting smooth line with nans
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
matplotlib Plotting smooth line with nans
#1
Hello,

I want to automate a chart (we call it snake charts, screenshot below) that so far we've been building in Excel.
It's a scatter plot with smoothed lines in between where the y-values are just rankings (1-2-3-4...) so that we can determine the order of the attributes.
It turns out that snake charts are not conventional (apparantly we made this thing up?) and I can't figure out how to smooth the line, knowing that we have nan's in the list.
My chart is ready, except for the lines between the dots, those should be smoothed if they can be (if they're connecting more than 2 dots)

I've read a lot of options on how to smooth lines, including that I should "mask" nan's
here: https://stackoverflow.com/questions/5283...ith-pyplot
and here: https://www.adamsmith.haus/python/answer...-in-python
and here: https://www.geeksforgeeks.org/how-to-plo...atplotlib/
and here: https://matplotlib.org/devdocs/gallery/l..._demo.html

...but none of those options seem to be able to solve my issue. Does anyone know how I can do this?
Note: in practice I can have 5 values, then a nan, and then 5 more values. So I can't simply skip the first nan.

import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
import pandas as pd
import numpy as np 

data = pd.DataFrame({"brand": ["a", "a", "a", "a", "b", "b", "b", "b"],
                     "attribute": ["attr1", "attr2", "attr3", "attr4", "attr1", "attr2", "attr3", "attr4"],
                     "score": [np.nan, 0.55, 0.25, 0.15, 0.26, 0.45, 0.20, 0.15],
                     "order": [1, 2, 3, 4, 1, 2, 3, 4]})

colours= pd.DataFrame({"brand": ["a", "b"], "hex_color": ["#859F84", "#F57921"]})

element_column = "brand"
elements = data[element_column].unique().tolist()

for element in elements:
    x = data.loc[data[element_column] == element, "score"]
    y = data.loc[data[element_column] == element, "order"]
    colour = colours.loc[colours[element_column] == element, "hex_color"].item()    
    y = np.ma.masked_where(np.isnan(y), y)
    plt.scatter(x, y, c=colour)
    plt.plot(x, y, c=colour)

labels = data[['attribute', 'order']].drop_duplicates().copy()
plt.yticks(labels["order"], labels["attribute"])
plt.show()
What I want:
[Image: 275566857_4846251875423126_7378958004873...e=623032F0]
Reply


Messages In This Thread
matplotlib Plotting smooth line with nans - by mikisDeWitte - Mar-10-2022, 07:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Graphic line plot with matplotlib, text file in pytho khadija 2 1,424 Aug-15-2022, 12:00 PM
Last Post: khadija
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,278 Mar-11-2021, 10:52 AM
Last Post: buran
  Multiple Line Chart Plotting moto17 1 2,524 Jan-20-2021, 01:38 PM
Last Post: wostan
  saving only one line of a figure as an image (python matplotlib) nitrochloric 0 2,058 Nov-23-2020, 01:41 PM
Last Post: nitrochloric
  Smooth curve in matplotlib medatib531 0 1,849 Apr-02-2020, 08:07 PM
Last Post: medatib531
  problem in plotting intraday results using matplotlib mr_byte31 0 2,974 Aug-20-2018, 11:32 AM
Last Post: mr_byte31

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020