import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 4, 9,16, 25]
plt.plot(x,y)
plt.show()
设置样式(添加标题,添加x,y的名称)
linewidth属性设置线条宽度
import matplotlib.pyplot as plt
x = range(-100, 100)
y = [i**2 for i in x]
plt.plot(x,y)
plt.savefig('result')
plt.show()
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
sin_y = np.sin(x)
plt.plot(x,sin_y)
plt.show()
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
sin_y = np.sin(x)
cos_y = np.cos(x)
plt.plot(x,sin_y)
plt.plot(x,cos_y)
plt.show()
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
plt.subplot(2, 2, 1)
plt.xlim(-5, 20)
plt.ylim(-2, 2)
plt.plot(x, np.sin(x))
plt.subplot(2, 2, 3)
plt.plot(x, np.cos(x))
plt.show()
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
plt.scatter(x, np.sin(x))
或者用plt.plot(x, np.sin(x),'o')
plt.show()
从上面的示例可以看到,使用plot绘制和使用scatter绘制出来的图形是没有区别的,但是使用plot绘制图形的速度由于scatter,所以如果画一堆点,而且点的形式没有差别,那么我们使用plot,如果点的形式有差别(指点的大小和颜色不同)则必须使用scatter
rand()指的是0-1之间的任意数,若想扩大可用*410222某个值
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(0)
x = np.random.rand(100)
y = np.random.rand(100)
size = np.arange(1000)|1000代表的是种类
size_1=np.random.choice(size,100,replace=False)不可重复
color = np.random.rand(100)
plt.scatter(x, y, s=size_1, c=color, alpha=0.7)s代表的是点的大小,c代表的是点的颜色,alpha代表的是点的透明度
plt.show()
print(size_1)
![在这里插入图片描述](https://img-blog.csdnimg.cn/36f71f4254524110aed28c106e50f14c.png
import matplotlib.pyplot as plt
x = [1980, 1985, 1990, 1995]
x_label = ['1980年', '1985年', '1990年', '1980年']
y = [1000, 2000, 3000, 4000]
width定宽度
plt.bar(x, y, width=3)
修改中文乱码:
plt.rcParams['font.sans-serif']=['SimHei']
修改横坐标
plt.xticks(x, x_label)
plt.xlabel('年份')
plt.ylabel('销量')
plt.title('根据年份')
plt.show()
import matplotlib.pyplot as plt
import numpy as np
plt.subplot(1, 2, 1)
np.random.seed(0)
x = np.arange(5)
y = np.random.randint(-5, 5, 5)
plt.bar(x, y, color='b')
v_bar = plt.bar(x, y, color='b')
*这里是给每条柱变颜色的*
for bar, height in zip(v_bar, y):
if height > 0:
bar.set(color='r')
plt.axhline(0, color='b', linewidth=2)
plt.subplot(1, 2, 2)
plt.barh(x, y, color='r')
plt.axvline(0, color='r', linewidth=2)
plt.show()
import matplotlib.pyplot as plt
import numpy as np
real_names = ['千与千寻', '玩具总动员', '黑衣人:全球追击']
real_num1 = [7548, 4013, 1673]
real_num2 = [5453, 1840, 1080]
real_num3 = [4348, 2345, 1980]
x = np.arange(len(real_names))
默认宽度为1,用0.3的话1-3*0.3=0.1,使中间有空格
width = 0.3
label是添加图例的一部分
plt.bar(x, real_num1, alpha=0.5,width=width, label=real_names[0])
plt.bar([i+width for i in x], real_num2, alpha=0.5, width=width, label=real_names[1])
plt.bar([i+width*2 for i in x], real_num3, alpha=0.5, width=width, label=real_names[2])
plt.rcParams['font.sans-serif']=['SimHei']
x_label=['第{}天'.format(i+1)for i in x]
plt.xticks([i+width for i in x], x_label)
plt.ylabel('票房数')
设置图例的:
plt.legend()
plt.title('三天三部电影的票房数')
plt.show()
import matplotlib.pyplot as plt
import numpy as np
man = 71351
women = 68187
man_perc = man/(women+man)
women_perc =women/(women+man)
plt.rcParams['font.sans-serif']=['SimHei']
labels = ['男', '女']
colors = ['blue', 'red']
#labels表示名称 coloes:颜色 explode=分裂 autopct 显示百分比
paches, tests, autotexts = plt.pie([man_perc, women_perc], labels=labels, colors=colors, explode=(0, 0.05), autopct='%0.1f%%')
#设置饼状图中的字体颜色
for text in autotexts:
text.set_color('white')
#设置字体大小
for text in tests+autotexts:
text.set_fontsize(20)
plt.show()
import matplotlib.pyplot as plt
import numpy as np
#生成1000个标准的正太分布随机数
x = np.random.randn(1000)
#修改柱的宽度 使用bins
plt.hist(x, bins=100)
plt.show()
loc 此概率分布的均值
scale 对应分布的跨百度,scale越大越矮胖,越小越瘦高
size 输出的shape,默认为None,只输出一个值
import matplotlib.pyplot as plt
import numpy as np
#使用np.random.normal()指定期望和均值的正太分布
x = np.random.normal(0, 0.8, 1000)
y = np.random.normal(-2, 1, 1000)
z = np.random.normal(3, 2, 1000)
kwargs = dict(bins = 100,alpha=0.5)#alpha是透明度,范围在0-1
plt.hist(x, **kwargs)
plt.hist(y, **kwargs)
plt.hist(z, **kwargs)
plt.show()
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10, 10, 100)
y = np.linspace(-10, 10, 100)
#计算x和y相交的点
X, Y = np.meshgrid(x, y)
#计算z的坐标
Z = np.sqrt(X**2+Y**2)
#绘制等高线图
plt.contour(X, Y, Z)(图一)
plt.contourf(X, Y, Z)(图二)
plt.show()
先导入3d包
import matplotlib.pyplot as plt
#导入3D包
from mpl_toolkits.mplot3d import Axes3D
#创建x,y,z
X = [1, 1, 2, 2]
Y = [3, 4, 4, 3]
Z = [1, 100, 1, 1]
#定义图像
figure = plt.figure()
#创建Axes3D对象
ax = Axes3D(figure)
ax.plot_trisurf(X, Y, Z)
plt.show()