1

say I create a dataclass for recording some data:

@dataclass
class HourlyReading:
    temperature:    float
    pressure:       int

I create a list of my dataclass instances and regularly add data to it:

stationA_meteorology = []
stationA_meteorology.append(HourlyReading(234,456))
stationA_meteorology.append(HourlyReading(345,345))
stationA_meteorology.append(HourlyReading(234,245))

Can I use my list as direct input to sns.lineplot to plot, for instance, a graph of temperature values?

3
  • @simon I don't understand what more details/clarity is needed here. Maybe you need to clarify. Commented Nov 11 at 19:08
  • You are addressing the wrong person: I did neither close your question, nor did I vote for closing it. All I did was edit your question (formatting the indentation of your code blocks), which is why you see my name here. In fact, I was already about to write an answer, but then couldn't, because your question got closed in the meantime. Commented Nov 11 at 20:17
  • I see. Unfortunately I can't find who closed it or why. Too bad SO is so user unfriendly and more concerned with pedantry than actually providing help. Commented Nov 11 at 20:44

2 Answers 2

2

You can't use dataclasses with seaborn, the easiest way is to convert them (or ideally store them as such) to pandas dataframes and then plot as normal, with seaborn.

However, if you insist on no external packages, you just need to split into separate x and y lists (of same size).


temps = [i.temperature for i in stationA_meteorology]
pressures = [i.pressure for i in stationA_meteorology]

# plot with seaborn
sns.scatterplot(x=temps, y=pressures)

The more standard method is using pandas:

import pandas as pd

# convert to a df like such
stationA_meteorology_df = pd.DataFrame(stationA_meteorology)

# plot with seaborn (it's enough to specify column name)
sns.scatterplot(x="temperature", y="pressure", 
                data=stationA_meteorology_df) 
Sign up to request clarification or add additional context in comments.

5 Comments

like the title says, I want to use built in stuff. Seaborn claims to work with "built-in Python types like lists and dictionaries. "
no problem, see my edited answer.
better but temp should be a y value. horizontal axis is time (readings taken every hour)
@monk For future reference, regarding the pandas approach: pandas supports dataclasses natively (in contrast to seaborn). So you can directly writepd.DataFrame(stationA_meteorology) and don't need to write pd.DataFrame([vars(s) for s in stationA_meteorology]).
Interesting, I edited my answer!
1

I don't think it is possible to use a dataclass.

Plain and nested lists work ...

import seaborn as sns

list_temp = [1, 2, 4, 8, 16, 32]
list_press = [1, 3, 9, 27, 81, 243]
hourly_reading = [list_temp, list_press]

sns.lineplot(list_temp)
sns.lineplot(data=hourly_reading)

... however you lose the series names. In the legend they will appear as generic index numbers (0, 1, 2 ...)

Dictionaries work better:

met_dict = {"Temperatures": list_temp, "Pressures": list_press}
sns.lineplot(met_dict)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.