Bar plots¶
In [1]:
Copied!
import sys
import matplotlib as mpl
import matplotlib.pyplot as plt
import plotly.express as px
import seaborn as sns
sys.path.append("..")
import arcadia_pycolor as apc
apc.mpl.setup()
apc.plotly.setup()
import sys
import matplotlib as mpl
import matplotlib.pyplot as plt
import plotly.express as px
import seaborn as sns
sys.path.append("..")
import arcadia_pycolor as apc
apc.mpl.setup()
apc.plotly.setup()
In [2]:
Copied!
num_reads = [
1_500_000,
1_200_000,
1_000_000,
400_000,
1_000_000,
1_700_000,
1_300_000,
600_000,
1_200_000,
1_500_000,
1_600_000,
300_000,
900_000,
1_700_000,
800_000,
]
sample_id = [
"AL 4M",
"EL 2W",
"EL 4W",
"EL 4W WGA",
"EL 12W",
"OM 2W",
"OM 4W",
"OM 4W WGA",
"OM 8W",
"WH 1M",
"WH 2M",
"WH 2M WGA",
"WH 4M",
"WH 2M Hous",
"WI 3W",
]
error = [
100000,
80000,
70000,
30000,
70000,
120000,
90000,
40000,
80000,
100000,
110000,
20000,
60000,
120000,
50000,
]
num_reads = [
1_500_000,
1_200_000,
1_000_000,
400_000,
1_000_000,
1_700_000,
1_300_000,
600_000,
1_200_000,
1_500_000,
1_600_000,
300_000,
900_000,
1_700_000,
800_000,
]
sample_id = [
"AL 4M",
"EL 2W",
"EL 4W",
"EL 4W WGA",
"EL 12W",
"OM 2W",
"OM 4W",
"OM 4W WGA",
"OM 8W",
"WH 1M",
"WH 2M",
"WH 2M WGA",
"WH 4M",
"WH 2M Hous",
"WI 3W",
]
error = [
100000,
80000,
70000,
30000,
70000,
120000,
90000,
40000,
80000,
100000,
110000,
20000,
60000,
120000,
50000,
]
1. Vertical bar chart using Matplotlib¶
In [3]:
Copied!
fig, ax = plt.subplots(figsize=apc.mpl.get_figure_dimensions("full_wide"), layout="constrained")
sample_id_formatted = [i.replace(" ", "\n") for i in sample_id]
plt.bar(sample_id_formatted, num_reads, color=apc.aster)
apc.mpl.style_plot(ax, monospaced_axes="y")
apc.mpl.set_xaxis_categorical()
# Use this to format the y-axis with commas.
ax.get_yaxis().set_major_formatter(mpl.ticker.FuncFormatter(lambda x, _: format(int(x), ","))) # type: ignore
# Remove this if you don't want error bars.
plt.errorbar(sample_id_formatted, num_reads, yerr=error, fmt="none", color=apc.crow)
plt.ylabel("Number of reads")
plt.xlabel("Sample")
plt.show()
fig, ax = plt.subplots(figsize=apc.mpl.get_figure_dimensions("full_wide"), layout="constrained")
sample_id_formatted = [i.replace(" ", "\n") for i in sample_id]
plt.bar(sample_id_formatted, num_reads, color=apc.aster)
apc.mpl.style_plot(ax, monospaced_axes="y")
apc.mpl.set_xaxis_categorical()
# Use this to format the y-axis with commas.
ax.get_yaxis().set_major_formatter(mpl.ticker.FuncFormatter(lambda x, _: format(int(x), ","))) # type: ignore
# Remove this if you don't want error bars.
plt.errorbar(sample_id_formatted, num_reads, yerr=error, fmt="none", color=apc.crow)
plt.ylabel("Number of reads")
plt.xlabel("Sample")
plt.show()
2. Horizontal bar chart using Matplotlib¶
In [4]:
Copied!
fig, ax = plt.subplots(figsize=apc.mpl.get_figure_dimensions("half_square"), layout="constrained")
plt.barh(sample_id, num_reads, color=apc.aster)
apc.mpl.style_plot(ax, monospaced_axes="x")
apc.mpl.set_yaxis_categorical()
ax.get_xaxis().set_major_formatter(mpl.ticker.FuncFormatter(lambda x, _: format(int(x), ","))) # type: ignore
plt.xticks(rotation=30, ha="right")
# Remove this if you don't want error bars.
plt.errorbar(num_reads, sample_id, xerr=error, fmt="none", color=apc.crow)
plt.xlabel("Number of reads")
plt.ylabel("Sample")
plt.show()
fig, ax = plt.subplots(figsize=apc.mpl.get_figure_dimensions("half_square"), layout="constrained")
plt.barh(sample_id, num_reads, color=apc.aster)
apc.mpl.style_plot(ax, monospaced_axes="x")
apc.mpl.set_yaxis_categorical()
ax.get_xaxis().set_major_formatter(mpl.ticker.FuncFormatter(lambda x, _: format(int(x), ","))) # type: ignore
plt.xticks(rotation=30, ha="right")
# Remove this if you don't want error bars.
plt.errorbar(num_reads, sample_id, xerr=error, fmt="none", color=apc.crow)
plt.xlabel("Number of reads")
plt.ylabel("Sample")
plt.show()
3. Vertical bar chart using Seaborn¶
In [5]:
Copied!
fig, ax = plt.subplots(figsize=apc.mpl.get_figure_dimensions("full_wide"), layout="constrained")
sns.barplot(
x=sample_id_formatted,
y=num_reads,
color=apc.aster,
saturation=1, # Seaborn by default desaturates the colors. This prevents that.
)
apc.mpl.style_plot(ax, monospaced_axes="y")
apc.mpl.set_xaxis_categorical()
ax.get_yaxis().set_major_formatter(mpl.ticker.FuncFormatter(lambda x, _: format(int(x), ","))) # type: ignore
plt.ylabel("Number of reads")
plt.xlabel("Sample")
plt.show()
fig, ax = plt.subplots(figsize=apc.mpl.get_figure_dimensions("full_wide"), layout="constrained")
sns.barplot(
x=sample_id_formatted,
y=num_reads,
color=apc.aster,
saturation=1, # Seaborn by default desaturates the colors. This prevents that.
)
apc.mpl.style_plot(ax, monospaced_axes="y")
apc.mpl.set_xaxis_categorical()
ax.get_yaxis().set_major_formatter(mpl.ticker.FuncFormatter(lambda x, _: format(int(x), ","))) # type: ignore
plt.ylabel("Number of reads")
plt.xlabel("Sample")
plt.show()
4. Horizontal bar chart using Seaborn¶
In [6]:
Copied!
fig, ax = plt.subplots(figsize=apc.mpl.get_figure_dimensions("half_square"), layout="constrained")
sns.barplot(
y=sample_id,
x=num_reads,
color=apc.aster,
saturation=1, # Seaborn by default desaturates the colors. This prevents that.
)
apc.mpl.style_plot(ax, monospaced_axes="x")
apc.mpl.set_yaxis_categorical()
ax.get_xaxis().set_major_formatter(mpl.ticker.FuncFormatter(lambda x, _: format(int(x), ","))) # type: ignore
plt.xticks(rotation=30, ha="right")
plt.xlabel("Number of reads")
plt.ylabel("Sample")
plt.show()
fig, ax = plt.subplots(figsize=apc.mpl.get_figure_dimensions("half_square"), layout="constrained")
sns.barplot(
y=sample_id,
x=num_reads,
color=apc.aster,
saturation=1, # Seaborn by default desaturates the colors. This prevents that.
)
apc.mpl.style_plot(ax, monospaced_axes="x")
apc.mpl.set_yaxis_categorical()
ax.get_xaxis().set_major_formatter(mpl.ticker.FuncFormatter(lambda x, _: format(int(x), ","))) # type: ignore
plt.xticks(rotation=30, ha="right")
plt.xlabel("Number of reads")
plt.ylabel("Sample")
plt.show()
5. Vertical bar chart using Plotly¶
In [7]:
Copied!
fig = px.bar(x=sample_id, y=num_reads, error_y=error)
fig.update_layout(
xaxis_title="Sample",
yaxis_title="Number of reads",
showlegend=False,
)
fig.update_traces(
marker_color=apc.aster,
error_y_color=apc.crow,
error_y_thickness=1.5,
error_y_width=0,
)
apc.plotly.style_plot(fig, categorical_axes="x", monospaced_axes="y")
apc.plotly.add_commas_to_axis_tick_labels(fig)
apc.plotly.set_figure_dimensions(fig, "full_wide")
fig.show()
fig = px.bar(x=sample_id, y=num_reads, error_y=error)
fig.update_layout(
xaxis_title="Sample",
yaxis_title="Number of reads",
showlegend=False,
)
fig.update_traces(
marker_color=apc.aster,
error_y_color=apc.crow,
error_y_thickness=1.5,
error_y_width=0,
)
apc.plotly.style_plot(fig, categorical_axes="x", monospaced_axes="y")
apc.plotly.add_commas_to_axis_tick_labels(fig)
apc.plotly.set_figure_dimensions(fig, "full_wide")
fig.show()
6. Horizontal bar chart using Plotly¶
In [8]:
Copied!
fig = px.bar(x=num_reads, y=sample_id, error_x=error)
fig.update_layout(
xaxis_title="Number of reads",
yaxis_title="Sample",
showlegend=False,
)
fig.update_traces(
marker_color=apc.aster,
error_x_color=apc.crow,
error_x_thickness=1.5,
error_x_width=0,
)
apc.plotly.style_plot(fig, categorical_axes="y", monospaced_axes="x")
apc.plotly.add_commas_to_axis_tick_labels(fig)
apc.plotly.set_figure_dimensions(fig, "half_square")
fig.show()
fig = px.bar(x=num_reads, y=sample_id, error_x=error)
fig.update_layout(
xaxis_title="Number of reads",
yaxis_title="Sample",
showlegend=False,
)
fig.update_traces(
marker_color=apc.aster,
error_x_color=apc.crow,
error_x_thickness=1.5,
error_x_width=0,
)
apc.plotly.style_plot(fig, categorical_axes="y", monospaced_axes="x")
apc.plotly.add_commas_to_axis_tick_labels(fig)
apc.plotly.set_figure_dimensions(fig, "half_square")
fig.show()
7. Bar chart with colored categories using Matplotlib¶
In [9]:
Copied!
fig, ax = plt.subplots(figsize=apc.mpl.get_figure_dimensions("full_wide"), layout="constrained")
color_labels = [idx.split(" ")[0] for idx in sample_id]
color_dict = dict(zip(set(color_labels), [apc.aster, apc.aegean, apc.amber, apc.seaweed, apc.rose]))
colors = [dict(color_dict)[i] for i in color_labels]
sample_id_formatted = [i.replace(" ", "\n") for i in sample_id]
plt.bar(sample_id_formatted, num_reads, color=colors)
apc.mpl.style_plot(ax, monospaced_axes="y")
apc.mpl.set_xaxis_categorical()
ax.get_yaxis().set_major_formatter(mpl.ticker.FuncFormatter(lambda x, _: format(int(x), ","))) # type: ignore
plt.ylabel("Number of reads")
plt.xlabel("Sample")
plt.show()
fig, ax = plt.subplots(figsize=apc.mpl.get_figure_dimensions("full_wide"), layout="constrained")
color_labels = [idx.split(" ")[0] for idx in sample_id]
color_dict = dict(zip(set(color_labels), [apc.aster, apc.aegean, apc.amber, apc.seaweed, apc.rose]))
colors = [dict(color_dict)[i] for i in color_labels]
sample_id_formatted = [i.replace(" ", "\n") for i in sample_id]
plt.bar(sample_id_formatted, num_reads, color=colors)
apc.mpl.style_plot(ax, monospaced_axes="y")
apc.mpl.set_xaxis_categorical()
ax.get_yaxis().set_major_formatter(mpl.ticker.FuncFormatter(lambda x, _: format(int(x), ","))) # type: ignore
plt.ylabel("Number of reads")
plt.xlabel("Sample")
plt.show()
8. Bar chart with colored categories using Seaborn¶
In [10]:
Copied!
fig, ax = plt.subplots(figsize=apc.mpl.get_figure_dimensions("full_wide"), layout="constrained")
color_labels = [idx.split(" ")[0] for idx in sample_id]
color_dict = dict(zip(set(color_labels), [apc.aster, apc.aegean, apc.amber, apc.seaweed, apc.rose]))
sns.barplot(
x=sample_id_formatted,
y=num_reads,
hue=color_labels,
palette=color_dict,
saturation=1, # Seaborn by default desaturates the colors. This prevents that.
)
apc.mpl.style_plot(ax, monospaced_axes="y")
apc.mpl.set_xaxis_categorical()
ax.get_yaxis().set_major_formatter(mpl.ticker.FuncFormatter(lambda x, _: format(int(x), ","))) # type: ignore
plt.ylabel("Number of reads")
plt.xlabel("Sample")
legend = ax.get_legend()
legend.set_title("ID")
legend.set_bbox_to_anchor((1.06, 1))
plt.show()
fig, ax = plt.subplots(figsize=apc.mpl.get_figure_dimensions("full_wide"), layout="constrained")
color_labels = [idx.split(" ")[0] for idx in sample_id]
color_dict = dict(zip(set(color_labels), [apc.aster, apc.aegean, apc.amber, apc.seaweed, apc.rose]))
sns.barplot(
x=sample_id_formatted,
y=num_reads,
hue=color_labels,
palette=color_dict,
saturation=1, # Seaborn by default desaturates the colors. This prevents that.
)
apc.mpl.style_plot(ax, monospaced_axes="y")
apc.mpl.set_xaxis_categorical()
ax.get_yaxis().set_major_formatter(mpl.ticker.FuncFormatter(lambda x, _: format(int(x), ","))) # type: ignore
plt.ylabel("Number of reads")
plt.xlabel("Sample")
legend = ax.get_legend()
legend.set_title("ID")
legend.set_bbox_to_anchor((1.06, 1))
plt.show()
9. Bar chart with colored categories using Plotly¶
In [11]:
Copied!
color_labels = [idx.split(" ")[0] for idx in sample_id]
color_dict = {
label: color
for label, color in zip(
set(color_labels), [apc.aster, apc.aegean, apc.amber, apc.seaweed, apc.rose]
)
}
fig = px.bar(
x=sample_id,
y=num_reads,
color=color_labels,
color_discrete_map=color_dict,
title=None,
)
fig.update_layout(
xaxis_title="Sample",
yaxis_title="Number of reads",
legend=dict(title="ID"),
)
apc.plotly.style_plot(fig, categorical_axes="x", monospaced_axes="y")
apc.plotly.style_legend(fig)
apc.plotly.add_commas_to_axis_tick_labels(fig)
apc.plotly.set_figure_dimensions(fig, "full_wide")
fig.show()
color_labels = [idx.split(" ")[0] for idx in sample_id]
color_dict = {
label: color
for label, color in zip(
set(color_labels), [apc.aster, apc.aegean, apc.amber, apc.seaweed, apc.rose]
)
}
fig = px.bar(
x=sample_id,
y=num_reads,
color=color_labels,
color_discrete_map=color_dict,
title=None,
)
fig.update_layout(
xaxis_title="Sample",
yaxis_title="Number of reads",
legend=dict(title="ID"),
)
apc.plotly.style_plot(fig, categorical_axes="x", monospaced_axes="y")
apc.plotly.style_legend(fig)
apc.plotly.add_commas_to_axis_tick_labels(fig)
apc.plotly.set_figure_dimensions(fig, "full_wide")
fig.show()