教你 4 种更快更简单的数据可视化的方法!
本文由瓜瓜整理自“机器之心”
在本文中,我们将看到如下 4 个数据可视化方法!
热力图
热力图(Heat Map)是数据的一种矩阵表示方法,其中每个矩阵元素的值通过一种颜色表示。不同的颜色代表不同的值,通过矩阵的索引将需要被对比的两项或两个特征关联在一起。热力图非常适合于展示多个特征变量之间的关系,因为你可以直接通过颜色知道该位置上的矩阵元素的大小。通过查看热力图中的其他点,你还可以看到每种关系与数据集中的其它关系之间的比较。颜色是如此直观,因此它为我们提供了一种非常简单的数据解释方式。
与「matplotlib」相比,「seaborn」可以被用于绘制更加高级的图形,它通常需要更多的组件,例如多种颜色、图形或变量。「matplotlib」可以被用于显示图形,「NumPy」可被用于生成数据,「pandas」可以被用于处理数据!绘图只是「seaborn」的一个简单的功能。
# Importing libs
import
import
import
import
# Create a random dataset
# Plot the heatmap
import
seaborn
as sns
import
pandas
as pd
import
numpy
as np
import
matplotlib.pyplot
as plt
# Create a random dataset
data = pd.DataFrame(np.random.random((
10,
6)), columns=[
"Iron Man",
"Captain America",
"Black Widow",
"Thor",
"Hulk",
"Hawkeye"])
print(data)
# Plot the heatmap
heatmap_plot = sns.heatmap(data, center=
0, cmap=
gist_ncar )
plt.show()
二维密度图
二维密度图(2D Density Plot)是一维版本密度图的直观扩展,相对于一维版本,其优点是能够看到关于两个变量的概率分布。当你有两个对输出非常重要的变量,并且希望了解它们如何共同作用于输出的分布时,用二维密度图观察数据是十分有效的。
# Importing libs
import
import
from
# Create the data
# Create and shor the 2D Density plot
import
seaborn
as sns
import
matplotlib.pyplot
as plt
from
scipy.stats
import skewnorm
# Create the data
speed = skewnorm.rvs(
4, size=
50)
size = skewnorm.rvs(
4, size=
50)
# Create and shor the 2D Density plot
ax = sns.kdeplot(speed, size, cmap=
"Reds", shade=
False, bw=
.15, cbar=
True)
ax.set(xlabel=
speed , ylabel=
size )
plt.show()
蜘蛛网图
蜘蛛网图(Spider Plot)是显示一对多关系的最佳方法之一。在蜘蛛网图中,一个变量相对于另一个变量的显著性是清晰而明显的,因为在特定的方向上,覆盖的面积和距离中心的长度变得更大。在下面的图表中,很容易比较复仇者联盟的不同属性,并看到他们各自的优势所在!
直接使用「matplotlib」而非「seaborn」来创建可视化结果。我们需要让每个属性沿圆周等距分布。我们将在每个角上设置标签,然后将值绘制为一个点,它到中心的距离取决于它的值/大小。最后,为了显示更清晰,我们将使用半透明的颜色来填充将属性点连接起来得到的线条所包围的区域。
# Import libs
import
import
import
import
# Get the data
# Get the data for Iron Man
# Make some calculations for the plot
# Plot stuff
import
pandas
as pd
import
seaborn
as sns
import
numpy
as np
import
matplotlib.pyplot
as plt
# Get the data
df=pd.read_csv(
"avengers_data.csv")
print(df)
"""
# Name Attack Defense Speed Range Health
0 1 Iron Man 83 80 75 70 70
1 2 Captain America 60 62 63 80 80
2 3 Thor 80 82 83 100 100
3 3 Hulk 80 100 67 44 92
4 4 Black Widow 52 43 60 50 65
5 5 Hawkeye 58 64 58 80 65
"""
# Get the data for Iron Man
labels=np.array([
"Attack",
"Defense",
"Speed",
"Range",
"Health"])
stats=df.loc[
0,labels].values
# Make some calculations for the plot
angles=np.linspace(
0,
2*np.pi, len(labels), endpoint=
False)
stats=np.concatenate((stats,[stats[
0]]))
angles=np.concatenate((angles,[angles[
0]]))
# Plot stuff
fig = plt.figure()
ax = fig.add_subplot(
111, polar=
True)
ax.plot(angles, stats,
o- , linewidth=
2)
ax.fill(angles, stats, alpha=
0.25)
ax.set_thetagrids(angles *
180/np.pi, labels)
ax.set_title([df.loc[
0,
"Name"]])
ax.grid(
True)
plt.show()
树状图
树状图(Tree Diagram)是自然而直观的,这使它们容易被解释。在下面的可视化结果中,根据 Kaggle 的统计数据(生命值、攻击力、防御力、特殊攻击、特殊防御、速度)绘制了一小部分口袋妖怪游戏的数据集的树状图。
在「Scipy」中计算和绘制树状图只需要一行简单的代码。
# Import libs
import
from
from
import
# Read in the dataset
# Drop any fields that are strings
# Only get the first 40 because this dataset is big
del
# Calculate the distance between each sample
# Orientation our tree
import
pandas
as pd
from
matplotlib
import pyplot
as plt
from
scipy.cluster
import hierarchy
import
numpy
as np
# Read in the dataset
# Drop any fields that are strings
# Only get the first 40 because this dataset is big
df = pd.read_csv(
Pokemon.csv )
df = df.set_index(
Name )
del
df.index.name
df = df.drop([
"Type 1",
"Type 2",
"Legendary"], axis=
1)
df = df.head(n=
40)
# Calculate the distance between each sample
Z = hierarchy.linkage(df,
ward )
# Orientation our tree
hierarchy.dendrogram(Z, orientation=
"left", labels=df.index)
plt.show()
原文链接:https://towardsdatascience.com/4-more-quick-and-easy-data-visualizations-in-python-with-code-da9030ab3429
阅读原文 最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。