简体   繁体   中英

matplotlib Violin Plot from pandas DF

I have a pandas data frame with two columns, ArrDelay (an integer value) and Month (an integer between 1 and 12 representing month).

I am trying to create a violin plot with a separate "violin" for each month.

However, using the following code:

flights_subset = flights[["ArrDelay","Month"]].values

fig, axes = plt.subplots()
axes.violinplot(flights_subset)
plt.show()

Gives me a plot with two "violins" one for ArrDelay and one for Month (I believe).

How can I create the desired plot?

I solved this using the following code:

flights_subset = []

for x in np.unique(flights[["Month"]].values):
    flights_subset.append(flights[flights["Month"] == x]["ArrDelay"].values)

fig, axes = plt.subplots()
axes.violinplot(flights_subset, positions=np.unique(flights[["Month"]].values))
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM