python绘制散点图、直方图、箱线图、散点图矩阵、小提琴图、回归图和关系类图
时间: 2023-10-26 10:05:25 浏览: 157
利用python绘制散点图
4星 · 用户满意度95%
以下是使用Python绘制常见的数据可视化图表的示例代码:
## 绘制散点图
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 3, 1, 4, 2]
plt.scatter(x, y)
plt.show()
## 绘制直方图
import numpy as np
import matplotlib.pyplot as plt
data = np.random.randn(1000)
plt.hist(data, bins=30)
plt.show()
## 绘制箱线图
import numpy as np
import matplotlib.pyplot as plt
data = np.random.randn(100, 5)
plt.boxplot(data)
plt.show()
## 绘制散点图矩阵
import seaborn as sns
import pandas as pd
iris = sns.load_dataset('iris')
sns.pairplot(iris, hue='species')
plt.show()
## 绘制小提琴图
import seaborn as sns
import pandas as pd
tips = sns.load_dataset('tips')
sns.violinplot(x="day", y="total_bill", hue="sex", data=tips)
plt.show()
## 绘制回归图
import seaborn as sns
import pandas as pd
tips = sns.load_dataset('tips')
sns.lmplot(x="total_bill", y="tip", data=tips)
plt.show()
## 绘制关系类图
import seaborn as sns
import pandas as pd
tips = sns.load_dataset('tips')
sns.relplot(x="total_bill", y="tip", hue="smoker", style="time", data=tips)
plt.show()
阅读全文