Matplotlib的安装与绘图

Python笔记本
546 0

代码

import matplotlib.pyplot as plt

# 绘制简单曲线
plt.plot([1, 3, 5], [4, 8, 10])
plt.show()

运行



代码

import matplotlib.pyplot as plt
import  numpy as np
x = np.linspace(-np.pi, np.pi, 100) #x轴定义域为 -3.14~3.14,取100个点
plt.plot(x, np.sin(x))
plt.show()

运行



代码

import matplotlib.pyplot as plt
import  numpy as np
x = np.linspace(-np.pi * 2, np.pi * 2, 100)
plt.figure(1, dpi=200) #创建图表
for i in range(1, 5):
    plt.plot(x, np.sin(x/i))
plt.show()

运行



代码

import matplotlib.pyplot as plt
import  numpy as np

#绘制直方图
plt.figure(1, dpi=300)
data = [1, 1, 1, 2, 2, 2, 3, 3, 4, 5, 5, 6, 4]
plt.hist(data)  #传入数据,直方图会统计数据出现的次数
plt.show()

运行



代码

import matplotlib.pyplot as plt
import numpy as np

### 绘制散点图
x = np.arange(1, 10)
y = x
fig = plt.figure()
plt.scatter(x, y, c = 'r', marker= 'o') #r 红色 o 圆点
plt.show()

运行



代码

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

iris = pd.read_csv("./iris_training.csv")
print(iris.head())

#绘制散点图
iris.plot(kind = "scatter", x = '120', y = '4')
plt.show()

运行



代码

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

iris = pd.read_csv("./iris_training.csv")
#设置样式
sns.set(style = "white", color_codes=True)
#设置绘制格式为散点图
sns.jointplot(x = "120", y = "4", data = iris, size = 5)
#displot绘制图形
sns.distplot(iris['120'])
plt.show()

运行



代码

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import warnings

warnings.filterwarnings("ignore")

iris = pd.read_csv("./iris_training.csv")
#设置样式
sns.set(style = "white", color_codes=True)
sns.FacetGrid(iris, hue="virginica").map(plt.scatter, "120", "4").add_legend()
plt.show()

运行

最后更新 2019-10-09
评论 ( 0 )
评论已关闭