Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

python - Plotting categorized data in Seaborn

I have categorized data. At specific dates I have data (A to E) that is counted every 15 minutes. When I want to plot with seaborn I get this:

enter image description here

Bigger bubbles cover smaller ones and the entire thing is not easy readable (e.g. 2020-05-12 at 21:15). Is it possible to display the bubbles for each 15-minute-class next to each other with a little bit of overlap?

My code:

import pandas as pd
import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt
import os

df = pd.read_csv("test_df.csv")
#print(df)



sns.set_theme()

sns.scatterplot(
  data = df,
  x = "date",
  y = "time",
  hue = "category",
  size = "amount",sizes=(15, 200)
)


plt.gca().invert_yaxis()


plt.show()

My CSV file:

date,time,amount,category
2020-05-12,21:15,13,A
2020-05-12,21:15,2,B
2020-05-12,21:15,5,C
2020-05-12,21:15,1,D
2020-05-12,21:30,4,A
2020-05-12,21:30,2,C
2020-05-12,21:30,1,D
2020-05-12,21:45,3,B
2020-05-12,22:15,4,A
2020-05-12,22:15,2,D
2020-05-12,22:15,9,E
2020-05-12,00:15,21,D
2020-05-12,00:30,11,E
2020-05-12,04:15,7,A
2020-05-12,04:30,1,B
2020-05-12,04:30,2,C
2020-05-12,04:45,1,A
2020-05-14,21:15,1,A
2020-05-14,21:15,5,C
2020-05-14,21:15,3,D
2020-05-14,21:30,4,A
2020-05-14,21:30,1,D
2020-05-14,21:45,5,B
2020-05-14,22:15,4,A
2020-05-14,22:15,11,E
2020-05-14,00:15,2,D
2020-05-14,00:30,11,E
2020-05-14,04:15,9,A
2020-05-14,04:30,11,B
2020-05-14,04:30,5,C
2020-05-14,05:00,7,A
question from:https://stackoverflow.com/questions/65545513/plotting-categorized-data-in-seaborn

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use a seaborn swarmplot for this. You first have to separate the "amount" column into separate entries, using .reindex and .repeat. Then you can plot.

Here is the code:

import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import os

df = pd.read_csv("test.csv")

df = df.reindex(df.index.repeat(df.amount))

sns.swarmplot(data = df, x = "date", y = "time", hue = "category")

plt.gca().invert_yaxis()

plt.show()

Here is the output: enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...