python - 提高 for 循环性能和自定义图表
问题描述
我创建了一个代码,它返回我所追求的输出 - 2 个图形,每个图形上有多条线。但是,代码很慢而且很大(就需要多少行代码而言)。我对我可以做的任何改进很感兴趣,这将帮助我更快地获得这样的图表,并使我的代码更美观。
此外,我想在我的图表中添加更多内容(轴名称和标题是我所追求的)。通常,我会使用plt.xlabel
,plt.ylabel
并plt.title
这样做,但是我不太明白如何在这里使用它们。这里的目的是在每个循环之后为每个图形添加一条线(我已经修改了这段代码来这样做)。
我应该注意,我需要使用 Python 来完成这项任务(所以我不能更改为其他任何东西)并且我确实需要 Sympy 库来查找在我的图表中绘制的值。
到目前为止,我的代码如下:
import matplotlib.pyplot as plt
import sympy as sym
import numpy as np
sym.init_printing()
x, y = sym.symbols('x, y') # defining our unknown probabilities
al = np.arange(20,1000,5).reshape((196,1)) # values of alpha/beta
prob_of_strA = []
prob_of_strB = []
colours=['r','g','b','k','y']
pen_values = [[0,-5,-10,-25,-50],[0,-25,-50,-125,-250]]
fig1, ax1 = plt.subplots()
fig2, ax2 = plt.subplots()
for j in range(0,len(pen_values[1])):
for i in range(0,len(al)): # choosing the value of beta
A = sym.Matrix([[10, 50], [int(al[i]), pen_values[0][j]]]) # defining matrix A
B = sym.Matrix([[pen_values[1][j], 50], [int(al[i]), 10]]) # defining matrix B
sigma_r = sym.Matrix([[x, 1-x]]) # defining the vector of probabilities
sigma_c = sym.Matrix([y, 1-y]) # defining the vector of probabilities
ts1 = A * sigma_c ; ts2 = sigma_r * B # defining our utilities
y_sol = sym.solvers.solve(ts1[0] - ts1[1],y,dict = True) # solving for y
x_sol = sym.solvers.solve(ts2[0] - ts2[1],x,dict = True) # solving for x
prob_of_strA.append(y_sol[0][y]) # adding the value of y to the vector
prob_of_strB.append(x_sol[0][x]) # adding the value of x to the vector
ax1.plot(al,prob_of_strA,colours[j],label = ["penalty = " + str(pen_values[0][j])]) # plotting value of y for a given penalty value
ax2.plot(al,prob_of_strB,colours[j],label = ["penalty = " + str(pen_values[1][j])]) # plotting value of x for a given penalty value
ax1.legend() # showing the legend
ax2.legend() # showing the legend
prob_of_strA = [] # emptying the vector for the next round
prob_of_strB = [] # emptying the vector for the next round
解决方案
您可以通过在循环内初始化空向量来节省几行代码。您不必费心在最后重新定义它们。
for j in range(0,len(pen_values[1])):
prob_of_strA = []
prob_of_strB = []
for i in range(0,len(al)): # choosing the value of beta
A = sym.Matrix([[10, 50], [int(al[i]), pen_values[0][j]]]) # defining matrix A
B = sym.Matrix([[pen_values[1][j], 50], [int(al[i]), 10]]) # defining matrix B
sigma_r = sym.Matrix([[x, 1-x]]) # defining the vector of probabilities
sigma_c = sym.Matrix([y, 1-y]) # defining the vector of probabilities
ts1 = A * sigma_c ; ts2 = sigma_r * B # defining our utilities
y_sol = sym.solvers.solve(ts1[0] - ts1[1],y,dict = True) # solving for y
x_sol = sym.solvers.solve(ts2[0] - ts2[1],x,dict = True) # solving for x
prob_of_strA.append(y_sol[0][y]) # adding the value of y to the vector
prob_of_strB.append(x_sol[0][x]) # adding the value of x to the vector
ax1.plot(al,prob_of_strA,colours[j],label = ["penalty = " + str(pen_values[0][j])]) # plotting value of y for a given penalty value
ax2.plot(al,prob_of_strB,colours[j],label = ["penalty = " + str(pen_values[1][j])]) # plotting value of x for a given penalty value
ax1.legend() # showing the legend
ax2.legend() # showing the legend
推荐阅读
- ios - 如何在 iOS 主屏幕小部件中显示 WkWebView 屏幕截图?
- python - 将数据框的列附加到列表中
- bash - 我使用“-print0”选项来处理带空格的文件名,但出现错误
- javascript - 如何修复字符串保存为代码到单词?
- c++ - 将函数指针 reinterpret_cast 与 void(*)() 进行比较是未定义的行为吗?
- c++ - 乘法期间的 C++ 运算顺序
- java - 将对象的arrayList 转换为同一对象的数组时,为什么会出现 ClassCastException?
- python-3.x - 使用带有 Python 的 swagger codegen 插件生成 API 客户端库
- javascript - 带有 Chrome 可执行文件的 Puppeteer 中的外部资源无法加载 (net::ERR_EMPTY_RESPONSE)
- c - gpiolib-sysfs.c 中使用的 dev_attrs 的定义