Download scripts/plot.py from DeepLearnPhysics/tpcpp-10m: direct link, hf CLI and curl.
- Browser
- Download file 6.31 kB
-
https://huggingface.co/datasets/DeepLearnPhysics/tpcpp-10m/resolve/main/scripts/plot.py
- Command line
-
hf download hf://datasets/DeepLearnPhysics/tpcpp-10m/scripts/plot.py
-
curl -L -o plot.py https://huggingface.co/datasets/DeepLearnPhysics/tpcpp-10m/resolve/main/scripts/plot.py
6.31 kB
| 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 | |