python使用numpy、pandas、matplotlib、seaborn画数据分析图

需要环境(打开cmd输入命令即可安装):
pip install numpy
pip install pandas
pip install matplotlib
pip install seaborn

代码一

点击查看代码
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# 柱形图
# 准备数据
x = np.array(['basketball', 'football', 'ping-pong', 'badminton'])
y = np.array([10, 20, 20, 9])
c = np.array(["#4caf50", "red", "hotpink", "#556b2f"])
# 插入数据
plt.bar(x, y, color=c, width=0.5)
plt.title('hobby')
plt.show()

代码二

点击查看代码
import numpy as np
import matplotlib.pyplot as plt

y = np.array([35, 25, 25, 15])
l = ['baseball', 'tennis', 'pingpong', 'others']
c = ['#d5695d', '#5d8ca8', '#65a479', "#a564c9"]
e = (0.1, 0.2, 0.3, 0.4)
# 插入数据
plt.pie(y, labels=l, colors=c, autopct='%.1f%%', explode=e)
plt.title('hobby')
plt.savefig('hobby.png')
plt.show()

代码三

点击查看代码
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# 散点数据准备
x = np.random.randn(1000)
y = np.random.randn(1000)

# matPlot画散点图
plt.scatter(x, y, marker='x')
plt.show()

# 用seaBorn画散点图
df = pd.DataFrame({'x': x, 'y': y})
sns.jointplot(x='x', y='y', data=df, kind='scatter')
plt.show()

# 折线图数据准备
x = [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]
y = [5, 3, 6, 20, 17, 16, 19, 30, 32, 35]

# 用matPlotLib画图
plt.plot(x, y)
plt.show()

# 用seaBorn画折线图
df = pd.DataFrame({'x': x, 'y': y})
sns.lineplot(x='x', y='y', data=df)
plt.show()

代码四

点击查看代码
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

x = np.array(['Cat1', 'Cat2', 'Cat3', 'Cat4', 'Cat5'])
y = np.array([5, 4, 8, 12, 7])

sns.barplot(x, y)
plt.show()

y = np.array([35, 25, 25, 15])
l = ['baseball', 'tennis', 'pingpong', 'others']
c = ['#d5695d', '#5d8ca8', '#65a479', "#a564c9"]
e = (0.1, 0.2, 0.3, 0.4)
sns.distplot(y, labels=l, colors=c, autopct='%.1f%%', explode=e)
plt.title('hobby')
plt.show()

posted @ 2022-05-08 16:37  N暖阳_李维宁  阅读(199)  评论(0编辑  收藏  举报