小编Ebr*_*rin的帖子

Plotly imshow 反转 y 标签反转图像

我想可视化一个 20x20 矩阵,其中左上角点是 (-10, 9),右下角是 (9, -10)。所以x从左到右递增,y从上到下递减。所以我的想法是将 x 标签作为列表传递:[-10, -9 ... 9, 9],将 y 标签作为 [9, 8 ... -9, -10] 传递。这在seaborn(matplotlib)中按预期工作,但是在plotly中这样做只会垂直反转图像。这是代码:

import numpy as np
import plotly.express as px

img = np.arange(20**2).reshape((20, 20))
fig = px.imshow(img,
            x=list(range(-10, 10)),
            y=list(range(-10, 10)),
            )
fig.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

import numpy as np
import plotly.express as px

img = np.arange(20**2).reshape((20, 20))
fig = px.imshow(img,
            x=list(range(-10, 10)),
            y=list(reversed(range(-10, 10))),
            )
fig.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

为什么会发生这种情况以及如何解决它?

编辑:添加seaborn代码以查看差异。如您所见,反转标签的范围只会更改标签,对图像没有任何影响,这就是我想要的效果。

import seaborn as sns
import numpy as np

img = np.arange(20**2).reshape((20, 20))

sns.heatmap(img, 
            xticklabels=list(range(-10, 10)), …
Run Code Online (Sandbox Code Playgroud)

python plotly plotly-python

10
推荐指数
1
解决办法
1167
查看次数

使用 Comparator.reversed() 反转流的排序顺序

我正在编写一个程序,读取一个字符串作为输入,然后输出 10 个最重复的单词。问题是,我的顺序颠倒了。我想从最高到最低输出,并且按相反顺序排序。所以我一直在寻找解决方案,我唯一发现的是 .reversed() 方法,但它说“不能从静态上下文引用非静态方法”。我不明白这个错误,因为在示例中它被用于类似的情况。

我是流新手,所以我想知道解决此问题的简单方法。

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        List<String> words = Arrays.asList(input.split(" "));
        words.stream()
                .map(word -> word.replaceAll("[^A-Za-z0-9]", ""))
                .map(String::toLowerCase)
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
                .entrySet()
                .stream()
                .sorted(Comparator.comparing(Map.Entry::getValue).reversed()) // static method can't be referenced
                .limit(10)
                .sorted(Comparator.comparing(Map.Entry::getKey))
                .forEach(e -> System.out.println(e.getKey()));
    }
Run Code Online (Sandbox Code Playgroud)

编辑:我已经将地图的键和值混合并进行了编辑。由于我的错误,Arvind 的答案可能看起来有点不同,但这并不重要,因为 Map.Entry 同时具有 .comparingByKey 和 .comparingByValue 方法。

java comparator java-8 java-stream

3
推荐指数
1
解决办法
1036
查看次数

标签 统计

comparator ×1

java ×1

java-8 ×1

java-stream ×1

plotly ×1

plotly-python ×1

python ×1