Python error: generating a scatter plot using matplotlib

Python error: generating a scatter plot using matplotlib

我是一个 python 新手,正在为如何在 matplotlib.pyplot 中导入 CSV 文件而苦恼 我想看看小时(=人们花多少小时玩视频游戏)和级别(=游戏级别)之间的关系。然后我想在 female(1) 和 male(0) 之间用不同颜色的 Tax 绘制一个散点图。所以,我的 x 将是 'hour',我的 y 将是 'level'。

我的数据 csv 文件如下所示:

          hour gender level
0            8    1   20.00
1            9    1   24.95
2           12    0   10.67
3           12    0   18.00
4           12    0   17.50
5           13    0   13.07
6           10    0   14.45
...
...
499         12    1  19.47
500         16    0  13.28

这是我的代码:

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

df=pd.read_csv('data.csv')
plt.plot(x,y, lavel='some relationship')
plt.title("Some relationship")
plt.xlabel('hour')
plt.ylabel('level')
plt.plot[gender(gender=1), '-b', label=female]
plt.plot[gender(gender=0), 'gD', label=male]
plt.axs()
plt.show()

我想画下图。所以,就会有男女两行。

y=level|           @----->male
       | @
       | *         *----->female
       |________________ x=hour

但是,我不确定如何解决这个问题。 我一直收到错误 NameError: name 'hour' is not defined。

可以这样做:

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

df = pd.DataFrame(data={"hour": [8,9,12,12,12,13,10], 
                        "gender": [1,1,0,0,0,0,0],
                        "level": [20, 24.95, 10.67, 18, 17.5, 13.07, 14.45]})

df.sort_values("hour", ascending=True, inplace=True)

fig = plt.figure(dpi=80)
ax = fig.add_subplot(111, aspect='equal')

ax.plot(df.hour[df.gender==1], df.level[df.gender==1], c="red", label="male")
ax.plot(df.hour[df.gender==0], df.level[df.gender==0], c="blue", label="female")
plt.xlabel('hour')
plt.ylabel('level')