python-matplotlib可视化工具 画散点图小案例练习

本文案例代码的编写在Jupyter Notebook上边进行

一、检查matplotlib是否安装好
! pip show matplotlib

在这里插入图片描述

二、画一个普通的散点图
import numpy as np
import matplotlib.pyplot as plt

N = 50
x = np.linspace(0.,10.,N)
y = np.sin(x)**2 + np.cos(x)

plt.figure()
plt.scatter(x,y,s=50,label = 'y = sin(x)**2 + cos(x)',color = 'y',marker = 'x') #s控制每个点的大小
plt.axis("equal")#轴比例相同
plt.xlabel('x')
plt.ylabel('y')
plt.legend()#显示图例
plt.show()

在这里插入图片描述

三、marker的样式
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(100)
N = 50
randx = np.random.random(N)*100
randy = np.random.random(N)*100

plt.figure(figsize=(7,6))
plt.scatter(randx,randy,marker = r'$\beta$',s = 150,color = 'darkorange')
plt.axis('equal')
plt.xlabel('randx')
plt.ylabel('randy')
#plt.show()
plt.tight_layout()

在这里插入图片描述

四、不同点大小不一样
import numpy as np
import matplotlib.pyplot as plt


np.random.seed(100)
N = 30
randx = np.random.random(N)*100
randy = np.random.random(N)*100
size = np.random.randint(50,200,size=N)
plt.scatter(randx,randy,s = size,color = 'darkorange')#点的大小不一样

plt.xlabel('randx')
plt.ylabel('randy')
plt.tight_layout()

在这里插入图片描述

五、添加子刻度
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(100)
N = 30
randx = np.random.random(N)*100
randy = np.random.random(N)*100
size = np.random.randint(50,200,size=N)
plt.scatter(randx,randy,s = size,color = 'darkorange')#点的大小不一样

from matplotlib.ticker import MultipleLocator
ax = plt.gca()
ax.xaxis.set_minor_locator(MultipleLocator(10))
ax.yaxis.set_minor_locator(MultipleLocator(10))#添加子刻度

plt.xlabel('randx')
plt.ylabel('randy')
plt.tight_layout()

在这里插入图片描述

六、不同点颜色不一样
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(100)
N= 30
randx = np.random.random(N)*100
randy = np.random.random(N)*100
size = np.random.randint(50,200,size = N)

plt.figure(figsize = (7,5))
plt.scatter(randx,randy,s = size,c = size,alpha = 0.8) #不同的点颜色而不一样

# ax = plt.gca()
# ax.xaxis.set_minor_locator(MultipleLocator(10))
# ax.yaxis.get_minor_locator(MultipleLocator(10))

plt.axis('equal')
plt.xlabel('randx')
plt.ylabel('randy')

plt.colorbar()

plt.show()

在这里插入图片描述

七、自己创建颜色
import numpy as np
import matplotlib.pyplot as plt

from matplotlib import cm
from matplotlib.colors import ListedColormap,LinearSegmentedColormap

top = cm.get_cmap('Oranges_r',128)
bottom = cm.get_cmap('Blues',128)

newcolors = np.vstack((top(np.linspace(0,1,128)),bottom(np.linspace(0,1,128))))

orange_blue = ListedColormap(newcolors,name = 'OrangeBlue')

np.random.seed(100)
N = 30
randx = np.random.random(N)*100
randy = np.random.random(N)*100
size = np.random.randint(50,200,size=N)

plt.figure(figsize = (7,5))
plt.scatter(randx,randy,s = size,c = size,alpha = 0.8,cmap = orange_blue)
plt.axis('equal')

ax = plt.gca()
ax.xaxis.set_minor_locator(MultipleLocator(10))
ax.yaxis.set_minor_locator(MultipleLocator(10))

plt.xlabel('randx')
plt.ylabel('randy')

plt.colorbar(label = 'circle size')

plt.show()

在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值