[英]How to Write a plt.scatter(x, y) function in one line where y=function of x
I was plotting a scatter plot to show null values in dataframe.我正在绘制散点图 plot 以显示 dataframe 中的 null 值。 As you can see the plt.scatter() function is not expressive enough.
如您所见, plt.scatter() function 的表现力不够。 Relation between list(range(0,1200)) and 'a' is not clear unless you see the previous lines.
除非您看到前面的行,否则 list(range(0,1200)) 和 'a' 之间的关系并不清楚。 Can the plt.scatter(x,y) be written in a more explicit way where it could be easily understood how x and y is related.
plt.scatter(x,y) 是否可以以更明确的方式编写,这样可以很容易地理解 x 和 y 是如何相关的。 Like if somebody only see the plt.scatter(x,y), they would understand what it is about.
就像如果有人只看到 plt.scatter(x,y),他们就会明白它的含义。
a = []
for i in range(0,1200):
feature_with_na = [feature for feature in df.columns if df[feature].isnull().sum()>i]
a.append(len(feature_with_na))
plt.scatter(list(range(0,1200)), a)
On your x axis you have the number, then on the y-axis you want to plot the number of columns in your DataFrame that have more than that number of null values.在 x 轴上你有数字,然后在 y 轴上你想要 plot DataFrame 中的列数超过 null 值的数量。
Instead of your loop you can count the number of null values within each column and use numpy.broadcasting
, ( [:, None]
), to compare with an array of your numbers.您可以计算每列中 null 值的数量,而不是循环,并使用
numpy.broadcasting
,( [:, None]
)与您的数字数组进行比较。 This allows you to specify an xarr
of the numbers, then you use that same array in the comparison.这允许您指定数字的
xarr
,然后在比较中使用相同的数组。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plot
df = pd.DataFrame(np.random.choice([1,2,3,4,5,np.NaN], (100,10)))
# Range of 'x' values to consider
xarr = np.arange(0, 100)
plt.scatter(xarr, (df.isnull().sum().to_numpy()>xarr[:, None]).sum(axis=1))
ALollz answer is good, but here's a less numpy-heavy alternative if that's your thing: ALollz 的回答很好,但如果这是你的事情,这里有一个不那么笨重的选择:
feature_null_counts = df.isnull().sum()
n_nulls = list(range(100))
features_with_n_nulls = [sum(feature_null_counts > n) for n in n_nulls]
plt.scatter(n_nulls, features_with_n_nulls)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.