You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Swarm plots show the distribution of values in a column by giving each entry one dot and adjusting the y-value so that dots do not overlap and appear symmetrically around the y=0 line. They complement histograms, box plots, and violin plots. This example could be generalized to implement a swarm plot for multiple categories by adjusting the y-coordinate for each category.
290
+
291
+
```python
292
+
import pandas as pd
293
+
import plotly.express as px
294
+
import collections
295
+
296
+
297
+
defswarm(
298
+
X_series,
299
+
point_size=16,
300
+
fig_width=800,
301
+
gap_multiplier=1.2,
302
+
):
303
+
#sorting will align columns in attractive arcs rather than having columns the vary unpredicatbly in the x-dimension
304
+
X_series=X_series.copy().sort_values()
305
+
306
+
307
+
# we need to reason in terms of the marker size that is measured in px
308
+
# so we need to think about each x-coordinate as being a fraction of the way from the
309
+
# minimum X value to the maximum X value
310
+
min_x =min(X_series)
311
+
max_x =max(X_series)
312
+
313
+
list_of_rows = []
314
+
# we will count the number of points in each "bin" / vertical strip of the graph
315
+
# to be able to assign a y-coordinate that avoids overlapping
316
+
bin_counter = collections.Counter()
317
+
318
+
for x_val in X_series:
319
+
# assign this x_value to bin number
320
+
# each bin is a vertical strip wide enough for one marker
df_iris = px.data.iris() # iris is a pandas DataFrame
373
+
swarm(df_iris["sepal_length"])
374
+
```
375
+
287
376
## Scatter and line plots with go.Scatter
288
377
289
378
If Plotly Express does not provide a good starting point, it is possible to use [the more generic `go.Scatter` class from `plotly.graph_objects`](/python/graph-objects/). Whereas `plotly.express` has two functions `scatter` and `line`, `go.Scatter` can be used both for plotting points (makers) or lines, depending on the value of `mode`. The different options of `go.Scatter` are documented in its [reference page](https://plotly.com/python/reference/scatter/).
0 commit comments