Создание диаграммы цветов в виде шкалы с использованием Python [duplicate]

нерекурсивная версия Java, которая просто добавляет элементы и перераспределяет их между возможными значениями. 0 'игнорируются и работают для фиксированных списков (то, что вам дано, это то, с чем вы можете играть) или список повторяемых чисел.

import java.util.*;

public class TestCombinations {

    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(0, 1, 2, 2, 5, 10, 20));
        LinkedHashSet<Integer> targets = new LinkedHashSet<Integer>() {{
            add(4);
            add(10);
            add(25);
        }};

        System.out.println("## each element can appear as many times as needed");
        for (Integer target: targets) {
            Combinations combinations = new Combinations(numbers, target, true);
            combinations.calculateCombinations();
            for (String solution: combinations.getCombinations()) {
                System.out.println(solution);
            }
        }

        System.out.println("## each element can appear only once");
        for (Integer target: targets) {
            Combinations combinations = new Combinations(numbers, target, false);
            combinations.calculateCombinations();
            for (String solution: combinations.getCombinations()) {
                System.out.println(solution);
            }
        }
    }

    public static class Combinations {
        private boolean allowRepetitions;
        private int[] repetitions;
        private ArrayList<Integer> numbers;
        private Integer target;
        private Integer sum;
        private boolean hasNext;
        private Set<String> combinations;

        /**
         * Constructor.
         *
         * @param numbers Numbers that can be used to calculate the sum.
         * @param target  Target value for sum.
         */
        public Combinations(ArrayList<Integer> numbers, Integer target) {
            this(numbers, target, true);
        }

        /**
         * Constructor.
         *
         * @param numbers Numbers that can be used to calculate the sum.
         * @param target  Target value for sum.
         */
        public Combinations(ArrayList<Integer> numbers, Integer target, boolean allowRepetitions) {
            this.allowRepetitions = allowRepetitions;
            if (this.allowRepetitions) {
                Set<Integer> numbersSet = new HashSet<>(numbers);
                this.numbers = new ArrayList<>(numbersSet);
            } else {
                this.numbers = numbers;
            }
            this.numbers.removeAll(Arrays.asList(0));
            Collections.sort(this.numbers);

            this.target = target;
            this.repetitions = new int[this.numbers.size()];
            this.combinations = new LinkedHashSet<>();

            this.sum = 0;
            if (this.repetitions.length > 0)
                this.hasNext = true;
            else
                this.hasNext = false;
        }

        /**
         * Calculate and return the sum of the current combination.
         *
         * @return The sum.
         */
        private Integer calculateSum() {
            this.sum = 0;
            for (int i = 0; i < repetitions.length; ++i) {
                this.sum += repetitions[i] * numbers.get(i);
            }
            return this.sum;
        }

        /**
         * Redistribute picks when only one of each number is allowed in the sum.
         */
        private void redistribute() {
            for (int i = 1; i < this.repetitions.length; ++i) {
                if (this.repetitions[i - 1] > 1) {
                    this.repetitions[i - 1] = 0;
                    this.repetitions[i] += 1;
                }
            }
            if (this.repetitions[this.repetitions.length - 1] > 1)
                this.repetitions[this.repetitions.length - 1] = 0;
        }

        /**
         * Get the sum of the next combination. When 0 is returned, there's no other combinations to check.
         *
         * @return The sum.
         */
        private Integer next() {
            if (this.hasNext && this.repetitions.length > 0) {
                this.repetitions[0] += 1;
                if (!this.allowRepetitions)
                    this.redistribute();
                this.calculateSum();

                for (int i = 0; i < this.repetitions.length && this.sum != 0; ++i) {
                    if (this.sum > this.target) {
                        this.repetitions[i] = 0;
                        if (i + 1 < this.repetitions.length) {
                            this.repetitions[i + 1] += 1;
                            if (!this.allowRepetitions)
                                this.redistribute();
                        }
                        this.calculateSum();
                    }
                }

                if (this.sum.compareTo(0) == 0)
                    this.hasNext = false;
            }
            return this.sum;
        }

        /**
         * Calculate all combinations whose sum equals target.
         */
        public void calculateCombinations() {
            while (this.hasNext) {
                if (this.next().compareTo(target) == 0)
                    this.combinations.add(this.toString());
            }
        }

        /**
         * Return all combinations whose sum equals target.
         *
         * @return Combinations as a set of strings.
         */
        public Set<String> getCombinations() {
            return this.combinations;
        }

        @Override
        public String toString() {
            StringBuilder stringBuilder = new StringBuilder("" + sum + ": ");
            for (int i = 0; i < repetitions.length; ++i) {
                for (int j = 0; j < repetitions[i]; ++j) {
                    stringBuilder.append(numbers.get(i) + " ");
                }
            }
            return stringBuilder.toString();
        }
    }
}

Пример ввода:

numbers: 0, 1, 2, 2, 5, 10, 20
targets: 4, 10, 25

Выход образца:

## each element can appear as many times as needed
4: 1 1 1 1 
4: 1 1 2 
4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1 1 1 2 10 10 
25: 1 2 2 10 10 
25: 5 10 10 
25: 1 1 1 1 1 20 
25: 1 1 1 2 20 
25: 1 2 2 20 
25: 5 20 
## each element can appear only once
4: 2 2 
10: 1 2 2 5 
10: 10 
25: 1 2 2 20 
25: 5 20
7
задан cphlewis 29 April 2016 в 00:21
поделиться

3 ответа

Да, но у морского берега есть жестко закодированный линейный локатор тика для цветной панели, поэтому результат может быть не совсем то, что вы хотите:

# http://matplotlib.org/examples/pylab_examples/pcolor_log.html
# modified to use seaborn

import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import numpy as np
from matplotlib.mlab import bivariate_normal
import seaborn as sns; sns.set()


N = 20
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]

# A low hump with a spike coming out of the top right.
# Needs to have z/colour axis on a log scale so we see both hump and spike.
# linear scale only shows the spike.
Z1 = bivariate_normal(X, Y, 0.1, 0.2, 1.0, 1.0) + 0.1 * bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)

fig, axs = plt.subplots(ncols=2)

sns.heatmap(Z1, ax = axs[0])
sns.heatmap(Z1, ax = axs[1],
            #cbar_kws={'ticks':[2,3]}, #Can't specify because seaborn does
            norm=LogNorm(vmin=Z1.min(), vmax=Z1.max()))


axs[0].set_title('Linear norm colorbar, seaborn')
axs[1].set_title('Log norm colorbar, seaborn')
plt.show()

См. пример pylab, который начался для pylab которая автоматически получает метки ярлыка colorbar (хотя в противном случае это не так).

Вы можете редактировать код морского кода, чтобы заставить его работать: если вы измените функцию plot() в /seaborn/matrix.py (ver 0.7 .0):

    # Possibly add a colorbar
    if self.cbar:
        ticker = mpl.ticker.MaxNLocator(6)
        if 'norm' in kws.keys():
            if type(kws['norm']) is mpl.colors.LogNorm:
                ticker = mpl.ticker.LogLocator(numticks=8)

вы получите:

Я предположим, что на морском github, но если вы хотите его раньше, вот оно.

8
ответ дан cphlewis 20 August 2018 в 07:54
поделиться
  • 1
    морская проблема github.com/mwaskom/seaborn/issues/459 также делает ее фиксируемой (по-другому) – cphlewis 6 May 2016 в 08:31
  • 2
    Единственное, если некоторые из ваших значений отрицательные, то это проблема. Гиперболический инверсный sin (arcsinh) работает лучше для отрицательных и 0 значений. – wordsforthewise 4 January 2018 в 05:57
  • 3
    В matplotlib нет встроенного arcsinh, но вы можете использовать SymLogNorm для отрицательных значений. Не уверен, что он обрабатывает 0. [Д0] matplotlib.org/devdocs/tutorials/colors/… – wordsforthewise 4 January 2018 в 06:01

Отвечая на cphlewis (у меня недостаточно репутации), я решил эту проблему, используя cbar_kws; как я увидел здесь: морская кластерная карта: установить цветные галочки .

Например cbar_kws={"ticks":[0,1,10,1e2,1e3,1e4,1e5]}.

s=np.random.rand(20,20)
sns.heatmap(s, norm=LogNorm(s.min(),s.max()),
            cbar_kws={"ticks":[0,1,10,1e2,1e3,1e4,1e5]},
            vmin = 0.001, vmax=10000)
plt.show()

Имейте приятный день.

0
ответ дан Jándrë 20 August 2018 в 07:54
поделиться
  • 1
    Откуда LogNorm? Кроме того, тепловая карта для sns.heatmap не показывает norm: что она делает? – selwyth 27 March 2018 в 20:28

Вы можете нормализовать значения на цветной панели с помощью matplotlib.colors.LogNorm . Мне также пришлось вручную установить метки на морском дне и в итоге появился следующий код:

#!/usr/bin/env python3

import math

import numpy as np
import seaborn as sn
from matplotlib.colors import LogNorm

data = np.random.rand(20, 20)

log_norm = LogNorm(vmin=data.min().min(), vmax=data.max().max())
cbar_ticks = [math.pow(10, i) for i in range(math.floor(math.log10(data.min().min())), 1+math.ceil(math.log10(data.max().max())))]

sn.heatmap(
    data,
    norm=log_norm,
    cbar_kws={"ticks": cbar_ticks}
)

heatmap rand [/g1]

0
ответ дан user2084795 20 August 2018 в 07:54
поделиться
Другие вопросы по тегам:

Похожие вопросы: