plotly / plotly/plotly.js

[BUG]: Violin plot width incorrectly calculated in grouped mode with multiple x-categories

Đang mở
#7,611 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

bug
Ngôn ngữ chính
JavaScript
Star
18.3k
Fork
2k
Merge trung bình
2 ngày 12 giờ
Pull request đã merge (30 ngày)
28

Mô tả

Description

When using violinmode: 'group' with violins at multiple x-axis categories, but without offsetgroup set, the width calculation uses the total number of traces across the entire plot instead of the number of traces at each x-position. This causes excessive empty space even when gap parameters are set to 0.

Screenshots/Video
Image
Steps to reproduce
<!DOCTYPE html>
<html>
<head>
	<!-- Load plotly.js into the DOM -->
	<script src='https://cdn.plot.ly/plotly-3.1.2.js'></script>
</head>
<body>
    <div id="myDiv"></div>
    <script>
        // Create data: 2 models × 3 evaluations = 6 traces total
        // But at each x-position, there are only 2 violins

        const models = ["Model A", "Model B"];
        const evals = ["Eval 1", "Eval 2", "Eval 3"];
        const colors = ["blue", "red"];

        const traces = [];

        models.forEach((model, modelIdx) => {
            evals.forEach((evalName, evalIdx) => {
                const yData = Array.from({length: 50}, () =>
                    Math.random() * 2 - 1 + modelIdx
                );

                traces.push({
                    type: 'violin',
                    x: Array(50).fill(evalName),
                    y: yData,
                    name: model,
                    legendgroup: model,
                    scalegroup: model,
                    // offsetgroup: model, // Uncomment to fix spacing
                    showlegend: evalIdx === 0,
                    line: { color: colors[modelIdx] }
                });
            });
        });

        const layout = {
            violinmode: 'group',
            violingap: 0,      // No gap between categories
            violingroupgap: 0, // No gap between groups
            title: 'Bug Demo: Excessive empty space despite gap=0'
        };

        Plotly.newPlot('myDiv', traces, layout);
    </script>
</body>
</html>
Notes
Root Cause

I think the issue is in src/traces/box/cross_trace_calc.js.

When traces are split across multiple x-categories (each trace appears at only one x-value) but offsetgroup is not set, the formula incorrectly uses the global trace count numTotal:

var num = nOffsetGroups || numTotal;
...
bdPos = dPos * groupFraction * groupGapFraction / num;

This should divide by the number of traces at each specific x-position, not the total number of traces in the plot.

Workaround

Uncommenting offsetgroup: model, below scalegroup: model, in the above code produces the expected behaviour with no space between violins. See the below image:
Image
However I don't believe this should be required - certainly it isn't mentioned in the docs.

Related

This probably also affects box plots since they share the width calculation logic in cross_trace_calc.js.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Inizia leggendo src/traces/box/cross_trace_calc.js e riproduci l’esempio con sei tracce violin, la modalità raggruppata e senza offsetgroup. Confronta l’allocazione della larghezza a ogni posizione x con l’attuale calcolo globale delle tracce, quindi verifica che la spaziatura delle tracce violin raggruppate corrisponda alle impostazioni con intervallo zero e valuta se la logica condivisa dei box-plot è interessata.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
javascript
Lĩnh vực
data-visualization
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
58/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.