Plot multiple boxplot in one graph
You should get your data in a specific format by melting your data (see below for how melted data looks like) before you plot. Otherwise, what you have done seems to be okay.
require(reshape2)
df <- read.csv("TestData.csv", header=T)
# melting by "Label". `melt is from the reshape2 package.
# do ?melt to see what other things it can do (you will surely need it)
df.m <- melt(df, id.var = "Label")
> df.m # pasting some rows of the melted data.frame
# Label variable value
# 1 Good F1 0.64778924
# 2 Good F1 0.54608791
# 3 Good F1 0.46134200
# 4 Good F1 0.79421221
# 5 Good F1 0.56919951
# 6 Good F1 0.73568570
# 7 Good F1 0.65094207
# 8 Good F1 0.45749702
# 9 Good F1 0.80861929
# 10 Good F1 0.67310067
# 11 Good F1 0.68781739
# 12 Good F1 0.47009455
# 13 Good F1 0.95859182
# 14 Good F1 1.00000000
# 15 Good F1 0.46908343
# 16 Bad F1 0.57875528
# 17 Bad F1 0.28938046
# 18 Bad F1 0.68511766
require(ggplot2)
ggplot(data = df.m, aes(x=variable, y=value)) + geom_boxplot(aes(fill=Label))
Edit: I realise that you might need to facet. Here's an implementation of that as well:
p <- ggplot(data = df.m, aes(x=variable, y=value)) +
geom_boxplot(aes(fill=Label))
p + facet_wrap( ~ variable, scales="free")
Edit 2: How to add x-labels
, y-labels
, title
, change legend heading
, add a jitter
?
p <- ggplot(data = df.m, aes(x=variable, y=value))
p <- p + geom_boxplot(aes(fill=Label))
p <- p + geom_jitter()
p <- p + facet_wrap( ~ variable, scales="free")
p <- p + xlab("x-axis") + ylab("y-axis") + ggtitle("Title")
p <- p + guides(fill=guide_legend(title="Legend_Title"))
p
Edit 3: How to align geom_point()
points to the center of box-plot? It could be done using position_dodge
. This should work.
require(ggplot2)
p <- ggplot(data = df.m, aes(x=variable, y=value))
p <- p + geom_boxplot(aes(fill = Label))
# if you want color for points replace group with colour=Label
p <- p + geom_point(aes(y=value, group=Label), position = position_dodge(width=0.75))
p <- p + facet_wrap( ~ variable, scales="free")
p <- p + xlab("x-axis") + ylab("y-axis") + ggtitle("Title")
p <- p + guides(fill=guide_legend(title="Legend_Title"))
p
Plot multiple boxplot in one graph in pandas or matplotlib?
Use return_type='axes'
to get a1.boxplot
to return a matplotlib Axes
object.
Then pass that axes to the second call to boxplot
using ax=ax
. This will cause both boxplots to be drawn on the same axes.
a1=a[['kCH4_sync','week_days']]
ax = a1.boxplot(by='week_days', meanline=True, showmeans=True, showcaps=True,
showbox=True, showfliers=False, return_type='axes')
a2 = a[['CH4_sync','week_days']]
a2.boxplot(by='week_days', meanline=True, showmeans=True, showcaps=True,
showbox=True, showfliers=False, ax=ax)
Plotting several boxplots from different dataframes in one graph
You could create a new dataframe, with a column for each of the given dataframes. Pandas will pad the columns with NaN
s to compensate for the different lengths.
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
df1 = pd.DataFrame({'Numbers': np.random.normal(.1, 1, np.random.randint(30, 100)).cumsum()})
df2 = pd.DataFrame({'Numbers': np.random.normal(.2, 1, np.random.randint(30, 100)).cumsum()})
df3 = pd.DataFrame({'Numbers': np.random.normal(.3, 1, np.random.randint(30, 100)).cumsum()})
df4 = pd.DataFrame({'Numbers': np.random.normal(.4, 1, np.random.randint(30, 100)).cumsum()})
combined_dfs = pd.DataFrame({'df1': df1['Numbers'],
'df2': df2['Numbers'],
'df3': df3['Numbers'],
'df4': df4['Numbers']})
sns.set_style('white')
sns.boxplot(data=combined_dfs, palette='flare')
sns.despine()
plt.show()
How to plot multiple boxplot in one graph for R 2020
What's wrong with just using boxplot
? No ggplot2
, and that should work in your version, too.
However, it's not unambiguous what you mean by "multiple box plots in one graph". Here three versions:
## by social group
op <- par(mfrow=c(1, 2)) ## set par
boxplot(Absenteeism.time.in.hours ~ Social.smoker, dat)
boxplot(Absenteeism.time.in.hours ~ Social.drinker, dat)
par(op) ## reset par
## by social group in one panel
datl <- reshape(dat, varying=2:3, direction="long")
boxplot(Absenteeism.time.in.hours ~ time + Social, datl)
## social group interaction
boxplot(Absenteeism.time.in.hours ~ ., dat)
Data:
dat <- read.table(header=T, text=" Absenteeism.time.in.hours Social.smoker Social.drinker
1 4 0 1
2 0 0 1
3 2 0 1
4 4 1 1
5 2 0 1
6 2 0 1
7 8 0 1
8 4 0 1
9 40 0 1
10 8 0 0
11 8 0 1
12 8 0 1
13 8 0 1
14 1 0 1
15 4 0 1
16 8 0 1
17 2 0 1
18 8 1 1
19 8 0 0
20 2 1 0")
Creating multiple boxplots on the same graph from a dictionary
my_dict = {'ABC': [34.54, 34.345, 34.761], 'DEF': [34.541, 34.748, 34.482]}
fig, ax = plt.subplots()
ax.boxplot(my_dict.values())
ax.set_xticklabels(my_dict.keys())
Create multiple boxplots from statistics in one graph
You can pass a list of dictionaries to the bxp
method. The easiest way to get such a list from your existing code is to put the dictionary construction inside a function and call it for each row of the dataframe.
Note that data.iloc[:, 0]
would be the first column, not the first row.
import matplotlib.pyplot as plt
import pandas as pd
def stats(row):
return {"med": row["sharpeRatio_med"],
"q1": row["sharpeRatio_q1"],
"q3": row["sharpeRatio_q3"],
"whislo": row["sharpeRatio_min"],
"whishi": row["sharpeRatio_max"]}
data = pd.DataFrame({"sharpeRatio_med": [3, 4, 2],
"sharpeRatio_q1": [2, 3, 1],
"sharpeRatio_q3": [4, 5, 3],
"sharpeRatio_min": [1, 1, 0],
"sharpeRatio_max": [5, 6, 4]})
fig, axes = plt.subplots()
axes.bxp([stats(data.iloc[i, :]) for i in range(len(data))],
showfliers=False)
plt.show()
plot multiple boxplot in one graph ( one column is list of data) by Plotly or other way
Here is the way I solve this problem.
nn = list_hour_df.explode('list_hour').reset_index(drop=True)
nn['list_hour'] = nn['list_hour'].astype(int)
nn
0 Active Life 22
1 Active Life 21
2 Active Life 18
3 Active Life 23
4 Active Life 20
... ... ...
396416 Shopping 8
396417 Shopping 13
396418 Shopping 21
396419 Shopping 19
396420 Shopping 4
fig = px.box(nn, x="list_hour", y="Main_Category")
fig.show()
enter image description here
Related Topics
Calculate Difference Between Values in Consecutive Rows by Group
How to Loop Through List and Create Separate Dataframes in R
How to Identify/Delete Non-Utf-8 Characters in R
Find All Combinations of a Set of Numbers That Add Up to a Certain Total
R: Error in Usemethod("Tbl_Vars")
How to Dplyr Rename a Column, by Column Index
Delete Rows Containing Specific Strings in R
Creating Grouped Bar-Plot of Multi-Column Data in R
How to Convert a Data Frame Column to Numeric Type
Break Dataframe into Smaller Dataframe'S and Save Them
How to Replace Negative Values in a Dataframe Column With a Different Value
Conditionally Remove Rows from a Database Using R
Adding Some Space Between the X-Axis and the Bars, in Ggplot
General Suggestions For Debugging in R
How to Join (Merge) Data Frames (Inner, Outer, Left, Right)