matplotlib 基础

In [ ]:
import numpy as np 
x = np.linspace(0, 10, 100)
In [ ]:
y = np.sin(x)
In [ ]:
import matplotlib as mpl
import matplotlib.pyplot as plt

plt.plot(x, y)
In [ ]:
plt.show()
In [ ]:
siny = y.copy()
In [ ]:
cosy = np.cos(x)
In [ ]:
plt.plot(x, siny)
plt.plot(x, cosy)
plt.show()
In [ ]:
plt.plot(x, siny)
plt.plot(x, cosy, color="red")
plt.show()
In [ ]:
plt.plot(x, siny)
plt.plot(x, cosy, color="red", linestyle="--")
plt.show()
In [ ]:
plt.plot(x, siny)
plt.plot(x, cosy, color="red", linestyle="--")
plt.xlim(-5, 15)
plt.ylim(0, 1)
plt.show()
In [ ]:
plt.plot(x, siny)
plt.plot(x, cosy, color="red", linestyle="--")
plt.axis([-1, 11, -2, 2])
plt.show()
In [ ]:
plt.plot(x, siny)
plt.plot(x, cosy, color="red", linestyle="--")
plt.xlabel("x axis")
plt.ylabel("y value")
plt.show()
In [ ]:
plt.plot(x, siny, label="sin(x)")
plt.plot(x, cosy, color="red", linestyle="--", label="cos(x)")
plt.xlabel("x axis")
plt.ylabel("y value")
plt.legend()
plt.show()
In [ ]:
plt.plot(x, siny, label="sin(x)")
plt.plot(x, cosy, color="red", linestyle="--", label="cos(x)")
plt.xlabel("x axis")
plt.ylabel("y value")
plt.legend()
plt.title("Welcome to matplotlib world!")
plt.show()

Scatter Plot

In [ ]:
plt.scatter(x, siny)
plt.show()
In [ ]:
plt.scatter(x, siny)
plt.scatter(x, cosy, color="red")
plt.show()
In [ ]:
x = np.random.normal(0, 1, 10000)
y = np.random.normal(0, 1, 10000)

plt.scatter(x, y, alpha=0.1)
plt.show()

 读取数据和简单的数据探索

In [ ]:
import numpy as np 
In [ ]:
import matplotlib as mpl
import matplotlib.pyplot as plt
In [ ]:
from sklearn import datasets
In [ ]:
iris = datasets.load_iris()
In [ ]:
iris.keys()
In [ ]:
print(iris.DESCR)
In [ ]:
iris.data
In [ ]:
iris.data.shape
In [ ]:
iris.feature_names
In [ ]:
iris.target
In [ ]:
iris.target.shape
In [ ]:
iris.target_names
In [ ]:
X = iris.data[:,:2]
In [ ]:
plt.scatter(X[:,0], X[:,1])
plt.show()
In [ ]:
y = iris.target
In [ ]:
plt.scatter(X[y==0,0], X[y==0,1], color="red")
plt.scatter(X[y==1,0], X[y==1,1], color="blue")
plt.scatter(X[y==2,0], X[y==2,1], color="green")
plt.show()
In [ ]:
plt.scatter(X[y==0,0], X[y==0,1], color="red", marker="o")
plt.scatter(X[y==1,0], X[y==1,1], color="blue", marker="+")
plt.scatter(X[y==2,0], X[y==2,1], color="green", marker="x")
plt.show()
In [ ]:
X = iris.data[:,2:]
In [ ]:
plt.scatter(X[y==0,0], X[y==0,1], color="red", marker="o")
plt.scatter(X[y==1,0], X[y==1,1], color="blue", marker="+")
plt.scatter(X[y==2,0], X[y==2,1], color="green", marker="x")
plt.show()


猜你喜欢

转载自blog.csdn.net/qq_27384769/article/details/80914579