简体   繁体   English

我们如何 label 矩阵 plot 与预定义的标签列表(例如素数列表)?

[英]How do we label a matrix plot with a predefined list of labels (e.g. a list of prime numbers)?

I have a CSV File with elliptic curve related data which looks as follows (excerpt):我有一个带有椭圆曲线相关数据的 CSV 文件,如下所示(摘录):

3,19,[(1,4,1),(4,7,44)]
3,13,[(4,1,10)]
5,11,[(4,39,14)]
3,7,[(1,2,1),(1,3622,111),(4,17,10)]
3,5,[(4,1,2),(4,959,124)]
3,23,[(3,5,2)]
5,13,[(2,7,2),(2,8,1),(2,47,70),...]
7,11,[(1,2,1),(1,13,2),(1,53,4),...]
3,29,[(4,13,4)]
5,17,[(2,2,3),(4,6881,498)]
5,19,[(4,71,18)]
3,37,[(4,25,14)]
7,17,[(4,19,30)]
7,19,[(3,66,5)]

The first and second number in each line forms a pair of odd prime numbers and the list after these two primes is used to specify the color of the corresponding matrix cell.每行 forms 中的第一个和第二个数字是一对奇数素数,这两个素数之后的列表用于指定相应矩阵单元的颜色。 I want to generate a matrix plot where the x-axis and y-axis are labeled with the first 200 (odd) primes: 3, 5, 7, 11, 13, 17, 19, 23, ..., 1193, 1201, 1213, 1217, 1223, 1229 .我想生成一个矩阵 plot ,其中 x 轴和 y 轴标有前 200 个(奇数)素数:3、5、7、11、13、17、19、23 3, 5, 7, 11, 13, 17, 19, 23, ..., 1193, 1201, 1213, 1217, 1223, 1229 The following code works fine to generate the colorized matrix plot.以下代码可以正常生成彩色矩阵 plot。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
from sympy import sieve, prime
import itertools
import pandas as pd

matrix_size = 200
matrix = np.zeros((matrix_size, matrix_size))

with open('C:/Users/esultano/git/elliptic_curves/data/elliptic_curves.csv') as f:
    for line in f.readlines():
        line = line.strip()
        p, q, cases = eval(line)
        idx_p = sieve.search(p)[0]-2
        idx_q = sieve.search(q)[0]-2
        if idx_p < matrix_size and idx_q < matrix_size:
            cases_set = set()
            for case in cases:
                cases_set.add(case[0])
            cases_list = list(cases_set)
            val = sum(i*i for i in cases_list)
            matrix[idx_p,idx_q] = val
            matrix[idx_q,idx_p] = val

max_prime = prime(matrix_size+2)
axis_labels = list(sieve.primerange(3, max_prime))

fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111)
matrix_plot = ax.matshow(matrix, interpolation='nearest')
ax.set_xticklabels(axis_labels)
ax.set_yticklabels(axis_labels)
plt.show()

The plot looks as follows: plot 如下所示:

在此处输入图像描述

Unfortunatelly the axis labeling is not correct since each axis should span the primes from 3 to 1229 .不幸的是,轴标记不正确,因为每个轴都应该跨越从31229的素数。 How can I fix the axis labeling?如何修复轴标签? I am not requiring to place each of these 200 primes as axis label (displaying a few labels would be fine).我不需要将这 200 个素数中的每一个都放置为轴 label(显示几个标签就可以了)。

As documentation of Axes.set_xticklabels() says, obligatory you have to call Axes.set_xticks() before.正如Axes.set_xticklabels()的文档所说,您必须先调用Axes.set_xticks()

I did necessary modifications to your code, so that it works now.我对您的代码进行了必要的修改,以便它现在可以工作。 Screenshot of output goes after code. output 的屏幕截图跟随代码。

In my code you can see // 9 , because of this 10 primes (ticks) on axis is shown.在我的代码中,您可以看到// 9 ,因为显示了轴上的 10 个素数(刻度)。 If you want eg 20 numbers then do // 19 , etc.如果您想要例如 20 个数字,则执行// 19等。

Try it online! 在线尝试! and See diffs查看差异

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
from sympy import sieve, prime
import itertools
import pandas as pd

matrix_size = 200
matrix = np.zeros((matrix_size, matrix_size))

with open('elliptic_curves.csv') as f:
    for line in f.readlines():
        line = line.strip()
        p, q, cases = eval(line)
        idx_p = sieve.search(p)[0]-2
        idx_q = sieve.search(q)[0]-2
        if idx_p < matrix_size and idx_q < matrix_size:
            cases_set = set()
            for case in cases:
                cases_set.add(case[0])
            cases_list = list(cases_set)
            val = sum(i*i for i in cases_list)
            matrix[idx_p,idx_q] = val
            matrix[idx_q,idx_p] = val

max_prime = prime(matrix_size+2)
axis_labels = list(enumerate(sieve.primerange(3, max_prime)))
axis_labels = axis_labels[::len(axis_labels) // 9][:-1] + [axis_labels[-1]]
ticks = [e[0] for e in axis_labels]
ticklabels = [e[1] for e in axis_labels]

fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111)
matrix_plot = ax.matshow(matrix, interpolation='nearest')
ax.set_xticks(ticks); ax.set_xticklabels(ticklabels)
ax.set_yticks(ticks); ax.set_yticklabels(ticklabels)
plt.show()

Output: Output:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 你如何在情节图例中显示文本标签? (例如删除图例中的标签行) - How do you just show the text label in plot legend? (e.g. remove a label's line in the legend) 如何检查Iterable中是否有任何奇数/偶数(例如列表/元组)? - How to check if there's any odd/even numbers in an Iterable (e.g. list/tuple)? 如何使用Python将电话号码列表(例如202.202.2020)格式化为(202)202-2020? - How to format a list of phone numbers e.g. 202.202.2020 into (202) 202-2020 using Python? 你如何获得 python 包的 pydoc 可访问路径列表,例如 numpy 或 tensorflow? - How do you get a list of pydoc accessible paths for a python package, e.g. numpy or tensorflow? 如何从 for 循环 output 创建结构(例如列表)? - How to Create a structure (e.g. a list) from a for loop output? 如何将字符串(例如'A')引用到更大列表的索引(例如['A','B','C','D',...])? - How can I reference a string (e.g. 'A') to the index of a larger list (e.g. ['A', 'B', 'C', 'D', ...])? 使用NLTK查找事物列表(例如河流列表) - Find a list of things (e.g. a list of rivers) using NLTK 如何在列表的值之间运行循环,例如 List = [0, 417, 2050, 2221, 3039] - How to run loop in between the values of list for e.g. List = [0, 417, 2050, 2221, 3039] 使用容器(例如元组或列表)进行 Numpy 切片 - Numpy slicing with container (e.g. tuple or list) 为什么分配给空列表(例如 [] = &quot;&quot;)不是错误? - Why isn't assigning to an empty list (e.g. [] = "") an error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM