在条件中为变量分配字符串会产生错误

时间:2019-07-24 13:50:55

标签: python pandas matplotlib

我想为散点图设置动态标记类型。所以,我有这样的东西:

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

x = pd.DataFrame(np.random.randint(20,40,size=(5,1)))
y = pd.DataFrame(np.random.randint(24,26,size=(5,1)))

if x <= 30:
marker_type = 'o' and fill_type = 'full' and label_type = "var <= 30"
if x > 30 and symbols_x <= 40:
marker_type = 'o' and fill_type = 'none' and label_type = "30 < var <= 40"

plot = plt.figure(1)
plt.scatter(x, y, marker=marker_type, fill=fill_type, label=label_type)
legend = plot.legend(loc='upper right')
plot.show()

但是我得到一个错误,显示:“ SyntaxError:无法分配给运算符” 有解决方案吗?

1 个答案:

答案 0 :(得分:0)

如果您按如下所示进行if循环,则此错误将消失。

if x <= 30:
    marker_type = 'o' 
    fill_type = 'full' 
    label_type = "var <= 30"
else:
    pass
if x > 30 and symbols_x <= 40:
    marker_type = 'o' 
    fill_type = 'none' 
    label_type = "30 < var <= 40"
else:
    pass

但是,您将整个数据帧X分配为“是”或“否”。因此,您将收到以下错误。 “ ValueError:DataFrame的真值不明确。请使用a.empty,a.bool(),a.item(),a.any()或a.all()。”