简体   繁体   English

如何在 y=x 的函数的一行中编写 plt.scatter(x, y) function

[英]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 ,然后在比较中使用相同的数组。

Sample Data样本数据

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)))

Code代码

# 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.

相关问题 如何修复“plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=y_test)”的错误 - How to fix the error of "plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=y_test)" plt.scatter()是冗余函数吗? - Is plt.scatter() a redundant function? matplotlib plt.scatter 显示错误的 x 轴数据 - matplotlib plt.scatter showing wrong x axis data 在散点图中添加 y=x 线 - Add the y=x line in a scatter graph 如何编写向量函数来应用操作 f(x,y)? - How to write a vector function to apply operation f(x,y)? 如何使 plt.scatter() 中的点一次显示一个? - How to make points in plt.scatter() appear one at a time on display? x,y作为散点图上的颜色密度的图函数? - Plot function of x, y as the colour density on a scatter plot? 如何在 seaborn scatter plot 中添加 x 和 y 轴线 - How to add x and y axis line in seaborn scatter plot 如何在单行中将Matlab函数max(y(x&gt; 3)&gt; 2)转换为numpy - how to translate matlab function max(y(x>3)>2) to numpy in single line 对于NxM数组,编写一个函数,使其在用户输入为(x,y)时应返回从(0,0)到(x,y)的所有值的总和。 其中x - For NxM array , Write a function such that if user input is (x,y) It should return a sum of all values from (0,0) to (x,y). where x<N & y<M
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM