当列数未知时,在 Pandas/Python 中为每个 df 列创建散点图
Creating scatterplot per df column in Pandas/Python, when number of columns is unknown
我是 Python 和 Whosebug 的新手。我正在尝试为每列 pd.dataframe 创建一个 combined/merged 散点图。
我的数据是这样的;
我想创建一个组合散点图,其中第一个 x-value 是作业 1 (string/header),相应的 y-values 是在列 'Assignment1' 中找到的值.我想为每个作业执行此操作,并将散点图合并到一个图中。
问题围绕着这样一个事实,即我不知道我必须处理多少列,所以我必须创建一个考虑到这一点的通用代码。但是我知道前两列由 ID 和名称组成,即 ([2:]) 我手动为数据框添加了 header 来给你一个数据示例,所以我也想问一下如何当一个人不知道列的总数时,一个名字 headers..?
如有任何想法,我们将不胜感激。
Python 3.6.1 64 位,Qt 5.6.2,Darwin 上的 PyQt5 5.6
欢迎使用 Whosebug!
您想要做的是遍历您的数据并将您的数据点附加到您可以绘制的列表中。
def plotXY(gradeMatrix):
#First two empty lists are defined
pointsx = []
pointsy = []
#The grades of the Matrix, of the valid data, is now made in to
#x and y coordinates, with a random small number added.
for grades in gradeMatrix:
for g in range(len(grades)):
#(note :I added small random values to differentiate the data
# points for viewing)
# append values found via the loop in the data to your created lists
# for x and y
pointsx.append(g + random()*0.2 - 0.1)
pointsy.append(float(grades[g]) + random()*0.2 - 0.1)
#The possible grades going op the y-axis are stored in a list
gradesy = [12,10,7,4,2,0,-3]
# Create data ticks, x values and y values for the plot.
plt.yticks(gradesy, [str(x) for x in gradesy])
plt.xticks(np.arange(10),np.arange(10))
# set range for the Y axis
plt.ylim(-4,13)
# create bar plot
plt.plot(pointsx,pointsy,'bs')
# Legend - labels
plt.title("Occurance of grades given for different assignments")
plt.ylabel("Grade")
plt.xlabel("Assignement number")
# show plot
plt.show()
我是 Python 和 Whosebug 的新手。我正在尝试为每列 pd.dataframe 创建一个 combined/merged 散点图。
我的数据是这样的;
我想创建一个组合散点图,其中第一个 x-value 是作业 1 (string/header),相应的 y-values 是在列 'Assignment1' 中找到的值.我想为每个作业执行此操作,并将散点图合并到一个图中。
问题围绕着这样一个事实,即我不知道我必须处理多少列,所以我必须创建一个考虑到这一点的通用代码。但是我知道前两列由 ID 和名称组成,即 ([2:]) 我手动为数据框添加了 header 来给你一个数据示例,所以我也想问一下如何当一个人不知道列的总数时,一个名字 headers..?
如有任何想法,我们将不胜感激。
Python 3.6.1 64 位,Qt 5.6.2,Darwin 上的 PyQt5 5.6
欢迎使用 Whosebug!
您想要做的是遍历您的数据并将您的数据点附加到您可以绘制的列表中。
def plotXY(gradeMatrix):
#First two empty lists are defined
pointsx = []
pointsy = []
#The grades of the Matrix, of the valid data, is now made in to
#x and y coordinates, with a random small number added.
for grades in gradeMatrix:
for g in range(len(grades)):
#(note :I added small random values to differentiate the data
# points for viewing)
# append values found via the loop in the data to your created lists
# for x and y
pointsx.append(g + random()*0.2 - 0.1)
pointsy.append(float(grades[g]) + random()*0.2 - 0.1)
#The possible grades going op the y-axis are stored in a list
gradesy = [12,10,7,4,2,0,-3]
# Create data ticks, x values and y values for the plot.
plt.yticks(gradesy, [str(x) for x in gradesy])
plt.xticks(np.arange(10),np.arange(10))
# set range for the Y axis
plt.ylim(-4,13)
# create bar plot
plt.plot(pointsx,pointsy,'bs')
# Legend - labels
plt.title("Occurance of grades given for different assignments")
plt.ylabel("Grade")
plt.xlabel("Assignement number")
# show plot
plt.show()