tikzplotly
tikzplotly copied to clipboard
Support histograms
It would be nice to support histograms such as:
import numpy as np
from typing import Optional
import plotly.express as px
def weighted_histogram(data, weights, num_bins: Optional[int] = None, **kwargs):
if num_bins is None:
_, bin_edges = np.histogram(data, bins="auto")
num_bins = len(bin_edges) - 1
bins = np.linspace(data.min(), data.max(), num=num_bins)
# Calculate counts for each bin
hist_counts = np.zeros(len(bins) - 1, dtype=int)
for i, value in enumerate(data):
factor = weights[i]
index = np.digitize(value, bins) - 1
if 0 <= index < len(hist_counts):
hist_counts[index] += factor
# Plot using px.bar, manually setting the x (bins) and y (counts)
bin_centers = (bins[:-1] + bins[1:]) / 2
fig = px.bar(x=bin_centers, y=hist_counts, **kwargs)
fig.update_layout(bargap=0)
# Iterate over each trace (bar chart) in the figure and adjust the marker properties
for trace in fig.data:
if trace.type == "bar": # Check if the trace is a bar chart
# Update marker properties to remove the border line or adjust its appearance
trace.marker.line.width = 0 # Set line width to 0 to remove the border
# Optionally, you can also set the line color to match the bar color exactly
trace.marker.line.color = trace.marker.color = "rgba(0, 0, 255, 1.0)"
return fig
data = np.random.normal(0, 1, 1000)
weights = (np.random.rand(1000) * 100).round()
fig1 = px.histogram(data, title="Regular Histogram")
fig2 = weighted_histogram(data, weights, num_bins=20, title="Weighted Histogram")
fig1.show()
fig2.show()
Equivalent in tikzplotlib:
import matplotlib.pyplot as plt
import numpy as np
import tikzplotlib
data = np.random.normal(0, 1, 1000)
weights = (np.random.rand(1000) * 100).round()
fig, ax = plt.subplots()
ax.hist(data, bins=20, weights=weights)
plt.title("Weighted Histogram with Matplotlib")
tikzplotlib.save('hist.tex')
print(open('hist.tex', 'r').read())
generated tex file
% This file was created with tikzplotlib v0.10.1.
\begin{tikzpicture}
\definecolor{darkgray176}{RGB}{176,176,176}
\definecolor{steelblue31119180}{RGB}{31,119,180}
\begin{axis}[
tick align=outside,
tick pos=left,
title={Weighted Histogram with Matplotlib},
x grid style={darkgray176},
xmin=-3.27443006685314, xmax=3.83205293637314,
xtick style={color=black},
y grid style={darkgray176},
ymin=0, ymax=6952.05,
ytick style={color=black}
]
\draw[draw=none,fill=steelblue31119180] (axis cs:-2.95140811216104,0) rectangle (axis cs:-2.62838615746893,170);
\draw[draw=none,fill=steelblue31119180] (axis cs:-2.62838615746893,0) rectangle (axis cs:-2.30536420277683,390);
\draw[draw=none,fill=steelblue31119180] (axis cs:-2.30536420277683,0) rectangle (axis cs:-1.98234224808473,766);
\draw[draw=none,fill=steelblue31119180] (axis cs:-1.98234224808473,0) rectangle (axis cs:-1.65932029339262,1314);
\draw[draw=none,fill=steelblue31119180] (axis cs:-1.65932029339262,0) rectangle (axis cs:-1.33629833870052,2310);
\draw[draw=none,fill=steelblue31119180] (axis cs:-1.33629833870052,0) rectangle (axis cs:-1.01327638400842,3355);
\draw[draw=none,fill=steelblue31119180] (axis cs:-1.01327638400842,0) rectangle (axis cs:-0.690254429316312,4742);
\draw[draw=none,fill=steelblue31119180] (axis cs:-0.690254429316312,0) rectangle (axis cs:-0.367232474624208,6044);
\draw[draw=none,fill=steelblue31119180] (axis cs:-0.367232474624208,0) rectangle (axis cs:-0.044210519932105,6621);
\draw[draw=none,fill=steelblue31119180] (axis cs:-0.044210519932105,0) rectangle (axis cs:0.278811434759998,6389);
\draw[draw=none,fill=steelblue31119180] (axis cs:0.278811434759998,0) rectangle (axis cs:0.601833389452102,5915);
\draw[draw=none,fill=steelblue31119180] (axis cs:0.601833389452102,0) rectangle (axis cs:0.924855344144206,4395);
\draw[draw=none,fill=steelblue31119180] (axis cs:0.924855344144206,0) rectangle (axis cs:1.24787729883631,2701);
\draw[draw=none,fill=steelblue31119180] (axis cs:1.24787729883631,0) rectangle (axis cs:1.57089925352841,2515);
\draw[draw=none,fill=steelblue31119180] (axis cs:1.57089925352841,0) rectangle (axis cs:1.89392120822052,2087);
\draw[draw=none,fill=steelblue31119180] (axis cs:1.89392120822052,0) rectangle (axis cs:2.21694316291262,971);
\draw[draw=none,fill=steelblue31119180] (axis cs:2.21694316291262,0) rectangle (axis cs:2.53996511760472,394);
\draw[draw=none,fill=steelblue31119180] (axis cs:2.53996511760472,0) rectangle (axis cs:2.86298707229683,280);
\draw[draw=none,fill=steelblue31119180] (axis cs:2.86298707229683,0) rectangle (axis cs:3.18600902698893,0);
\draw[draw=none,fill=steelblue31119180] (axis cs:3.18600902698893,0) rectangle (axis cs:3.50903098168103,246);
\end{axis}
\end{tikzpicture}
images