我有一個代碼可以生成 0 和 1 的隨機矩陣,我想將這些矩陣轉換為散點圖,其中坐標對應于矩陣行/列,散點的顏色對應于值(紅色如果為 0,例如如果為 1,則為藍色)。
我已經能夠用 matplotlib 做到這一點,但我的用例涉及生成數千張這樣的影像,而 matplotlib 為此目的非常慢。出于這個原因,我一直在嘗試使用 pyctgraph,但遇到了一些麻煩。
Matplotlib 代碼:
import itertools
import random
import numpy as np
import matplotlib.pyplot as plt
d = 25
w = 10
l = 5
for n in range(num):
lst = list(itertools.repeat(1, d 1)) list(itertools.repeat(0, d - 1))
random.shuffle(lst)
a = np.array(lst).reshape((w, l))
for i in range(w):
for j in range(l):
if a[i, j] == 1:
plt.scatter(i 1, j 1, c="red")
else:
plt.scatter(i 1, j 1, c="blue")
plt.savefig(path)
plt.clf()
Pyctgraph 代碼嘗試:
import pyqtgraph as pg
import pyqtgraph.exporters
import numpy as np
import itertools
import random
w = 10
l = 5
d = 25
for n in range(num):
plt=pg.plot()
lst = list(itertools.repeat(1, d 1)) list(itertools.repeat(0, d - 1))
random.shuffle(lst)
a = np.array(lst).reshape((w, l))
for i in range(w):
for j in range(l):
if a[i, j] == 1:
p=pg.ScatterPlotItem([i 1], [j 1],brush=None)
plt.addItem(p)
else:
p = pg.ScatterPlotItem([i 1], [j 1], brush=None)
plt.addItem(p)
exporter = pg.exporters.ImageExporter(plt.plotItem)
exporter.parameters()['width'] = 100
exporter.export('fileName.png')
pyctgraph 代碼運行但非常緩慢,所以由于我對包不熟悉,我一定是做錯了什么。感謝您的任何幫助!
編輯:澄清一下,所需的最終產品是實心點網格,用空格分隔它們。紅點的數量需要為 26,藍點的數量為 24,隨機打亂順序。
uj5u.com熱心網友回復:
我認為使用嵌套回圈并plt.scatter在回圈內運行是您的程式浪費大量時間的地方。最好只運行 plt.scatter 一次,而是通過meshgrid隨機打亂顏色的 (x,y) 坐標。
例如,我可以在沒有任何回圈或條件的情況下生成相同的圖,并且我只需要plt.scatter為每個點呼叫一次而不是 5x10 = 50 次(!)
x = np.arange(1,w 1)
y = np.arange(1,l 1)
xx,yy = np.meshgrid(x,y)
colors = ['r']*26 ['b']*24
random.shuffle(colors)
plt.scatter(xx,yy,color=colors)

我添加了一些基準測驗來展示我們正在查看的性能改進:
import itertools
import random
import numpy as np
import matplotlib.pyplot as plt
d = 25
w = 10
l = 5
## original program using matplotlib and nested loops
def make_matplotlib_grid():
lst = list(itertools.repeat(1, d 1)) list(itertools.repeat(0, d - 1))
random.shuffle(lst)
a = np.array(lst).reshape((w, l))
for i in range(w):
for j in range(l):
if a[i, j] == 1:
plt.scatter(i 1, j 1, c="red")
else:
plt.scatter(i 1, j 1, c="blue")
## using numpy mesh grid
def make_matplotlib_meshgrid():
x = np.arange(1,w 1)
y = np.arange(1,l 1)
xx,yy = np.meshgrid(x,y)
colors = ['r']*26 ['b']*24
random.shuffle(colors)
plt.scatter(xx,yy,color=colors)
## benchmarking to compare speed between the two methods
if __name__ == "__main__":
import timeit
n_plots = 10
setup = "from __main__ import make_matplotlib_grid"
make_matplotlib_grid_time = timeit.timeit("make_matplotlib_grid()", setup=setup, number=n_plots)
print(f"original program creates {n_plots} plots with an average time of {make_matplotlib_grid_time / n_plots} seconds")
setup = "from __main__ import make_matplotlib_meshgrid"
make_matplotlib_meshgrid_time = timeit.timeit("make_matplotlib_meshgrid()", setup=setup, number=n_plots)
print(f"numpy meshgrid method creates {n_plots} plots with average time of {make_matplotlib_meshgrid_time / n_plots} seconds")
print(f"on average, the numpy meshgrid method is roughly {make_matplotlib_grid_time / make_matplotlib_meshgrid_time}x faster")
輸出:
original program creates 10 plots with an average time of 0.1041847709 seconds
numpy meshgrid method creates 10 plots with average time of 0.003275972299999985 seconds
on average, the numpy meshgrid method is roughly 31.80270202528894x faster
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/424612.html
標籤:Python 表现 matplotlib pyqtgraph
