File size: 6,308 Bytes
2f13ed1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | import numpy as np
import plotly.express as px
import plotly.graph_objects as go
def plot_histogram(data,
nbins=100,
mean_color='coral',
bar_color='skyblue',
xaxis_title=None,
log_y=False):
# add bar plot
fig = px.histogram(data,
nbins=100,
title=None,
color_discrete_sequence=[bar_color])
# whether to use a log scale for the y-axis
if log_y:
fig.update_yaxes(type="log", title="Count")
fig.update_layout(bargap=0.2) # gaps between bar
fig.update_traces(showlegend=False) # remove legend for bars
# add a line for mean
mean = np.mean(data)
fig.add_vline(x=mean,
line_width=3,
line_dash="dot",
line_color=mean_color)
# add a dummy trace so we can have a legend for the mean value
fig.add_trace(go.Scatter(x=[mean],
y=[0],
mode="lines",
line=dict(color=mean_color, width=3, dash="dot"),
name=f"Mean={mean:.2f}",
showlegend=True))
if xaxis_title is None:
xaxis_title = 'Number'
fig.update_layout(
width=450,
height=350,
xaxis_title=xaxis_title,
yaxis_title="Count",
legend=dict(
x=0.95, # horizontal position (0 = left, 1 = right)
y=0.95, # vertical position (0 = bottom, 1 = top)
xanchor="right",
yanchor="top",
bgcolor="rgba(255,255,255,0.3)", # optional: semi-transparent background
title=None,
),
margin=dict(
l=50, # left margin
r=50, # right margin
t=50, # top margin
b=50 # bottom margin
)
)
return fig
def plot_pie(names,
fractions,
title = None,
width = 450,
height = 450):
fig = px.pie(
names = names,
values = fractions,
title = title,
category_orders={"names": names},
color_discrete_sequence=px.colors.qualitative.Pastel
)
fig.update_layout(
width = width,
height = height,
margin = dict(l=20, r=10, t=50, b=10))
return fig
def plot_scatter(x, y, z,
labels = None,
color_cycle = None, *,
marker_size = 2,
opacity = 1,
width = 600,
height = 480,
shuffle = False,
names = None,
axis_title_font_size = 16,
axis_tick_font_size = 12,
legend_config = None,
showticklabels = True,
unique_labels = None,
distance = 1):
# set up parameters
if labels is None:
labels = [1] * len(x)
if color_cycle is None:
color_cycle = color_discrete_sequence=px.colors.qualitative.Set3
if unique_labels is None:
unique_labels = np.unique(labels)
if shuffle:
np.random.shuffle(unique_labels)
if names is not None:
assert len(unique_labels) <= len(names)
else:
names = [None] * len(unique_labels)
# == create one trace for a unique label value ============
traces = []
for i, (label, name) in enumerate(zip(unique_labels, names)):
color = color_cycle[i % len(color_cycle)]
trace = go.Scatter3d(
x=x[labels == label],
y=y[labels == label],
z=z[labels == label],
mode = 'markers',
marker = dict(size = marker_size,
color = color,
opacity = opacity),
name = name,
showlegend = False
)
traces.append(trace)
# Dummy trace for larger legend marker
if name is not None:
trace = go.Scatter3d(
x=[None],
y=[None],
z=[None], # Invisible point
mode = 'markers',
name = name,
marker = dict(size = 12,
color = color), # Larger size for legend
showlegend = True
)
traces.append(trace)
# == figure layout ========================================
def config_axis(title):
return dict(title = dict(text = title,
font = dict(size=axis_title_font_size)),
tickfont = dict(size = axis_tick_font_size),
showticklabels = showticklabels)#,
# backgroundcolor='lightgray')
layout = go.Layout(
width=width,
height=height,
scene=dict(
xaxis = config_axis('x (cm)' if showticklabels else ''),
yaxis = config_axis('y (cm)' if showticklabels else ''),
zaxis = config_axis('z (cm)' if showticklabels else ''),
camera = dict(
# zoom (default is (1.25, 1.25, 1.25))
eye=dict(x=1.8 * distance, y=1.2 * distance, z=1.6 * distance),
# y axis is pointing up, so the z axis can lie down
up=dict(x=0, y=1, z=0)
)
),
# paper_bgcolor='rgba(0,0,0,0)',
# plot_bgcolor='rgba(0,0,0,0)',
margin = dict(l=0, r=0, t=0, b=20),
legend=dict(
x=0.80, # horizontal position (0 = left, 1 = right)
y=0.80, # vertical position (0 = bottom, 1 = top)
xanchor="left",
yanchor="top",
bgcolor="rgba(200,200,200,0.3)", # optional: semi-transparent background
title=None,
),
)
figure = go.Figure(data=traces, layout=layout)
if legend_config is not None:
figure.update_layout(legend=legend_config)
return figure
|