不使用matplotlib绘制轨迹图

时间:2019-12-20 18:28:42

标签: python

我正在尝试绘制轨迹线,我想创建一个白色图像并在顶部绘制轨迹。我想避免使用matplotlib,因为稍后我必须对结果进行其他一些操作。

我的问题是XY值浮动并且出现此错误:

  

IndexError:只有整数,切片(:),省略号(...),numpy.newaxis(None)和整数或布尔数组都是有效索引

如何在不丢失信息的情况下将这些值“标准化”为整数?

这是一个坐标[0.714169939757303, 0.285830060242697]的示例。

我的代码:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

df = ...
img = np.ones((100, 200, 3))
for r in range(df.shape[0]):
    matrix_XYZ = np.array(df.loc[r][1:])
    denom = np.sum(matrix_XYZ)
    X = df.loc[r][1]
    Y = df.loc[r][2]
    x = X / denom
    y = Y / denom
    coor = [x, y]
    img[coor[1], coor[0], :] = (255, 0, 0)

plt.imshow(img)
plt.show()

非常感谢您。

1 个答案:

答案 0 :(得分:1)

您需要将点四舍五入到最接近的像素才能实际绘制它们。您无法避免丢失信息。一个图只能以其像素分辨率为准。 (您可以尝试在像素之间绘制一个点,方法是将像素之间的两个像素设置为50%,而不是一个像素设置为100%,也可以尝试在LCD上渲染字体时进行“亚像素平滑”,但是这些技巧可能不值得。)

您还可能希望坐标以像素为单位,而不是介于0和1之间,因为将它们除以matrix_XYZ之和即可。

此外,如果我正确地说您的图像应该是0、255的R,G,B,那么每种颜色的图像都将具有1/255的亮度。

尝试这样的事情:

# Set image dimensions. We scale all the numbers to use the 
# full axis range, so your data will be stretched if this isn't square.
width = 100
height = 200
# Start with all white: (255, 255, 255) everywhere
# Note that imshow expects (col, row, channel) indexing
img = np.ones((height, width, 3)) * 255
for r in range(df.shape[0]):
    matrix_XYZ = np.array(df.loc[r][1:])
    # We're going to divide the numbers by the total of X, Y, and Z.
    # This isn't the same as normalizing to length 1 
    # (which would be dividing by the square root of the sum of the squares).
    # It's also not the same as subtracting the minimum
    # and dividing by the maximum minus the minimum in each dimension, 
    # like you would do for a normal auto-fit to axes.
    # Are you sure this is what you want?
    denom = np.sum(matrix_XYZ)
    X = df.loc[r][1]
    Y = df.loc[r][2]
    x = X / denom
    y = Y / denom
    # Now you have X and Y both on range 0-1
    # Convert to pixels
    coor = [int(round(x * width)), int(round(y * height))]
    # Set the pixel to red only.
    # I'm not sure about fancy slice assignment, so do each channel separately.
    # Also note that imshow expects (col, row, channel) indexing, so Y first.    
    img[coor[1], coor[0], 0] = 255
    img[coor[1], coor[0], 1] = 0
    img[coor[1], coor[0], 2] = 0

# imshow is still Matplotlib, so this isn't really "not using Matplotlib".
plt.imshow(img)
plt.show()
相关问题