美文网首页
Matplotlib可视化---散点图

Matplotlib可视化---散点图

作者: 照希 | 来源:发表于2023-03-16 16:09 被阅读0次

今天总结下Matplotlib的散点图。

## pip install matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

利用numpy创建一组数组,做为y轴。

num=100
np.random.seed(3)
x=np.array(range(1,num+1))
y=1.2*x+4+np.random.randn(num)*5
z=1.3*x+10+np.random.randn(num)*6

直接先画图,再做调整。

image.png
散点图常用参数总结
matplotlib.pyplot.scatter(x, y,
s=20,
c='b',
marker='o',
alpha=None)

x,y:表示的是shape大小为(n,)的数组,也就是我们即将绘制散点图的数据点,输入数据。
s:表示的是大小,是一个标量或者是一个shape大小为(n,)的数组,可选,默认20。
c:表示的是色彩或颜色序列,可选,默认蓝色’b’。但是c不应该是一个单一的RGB数字,也不应该是一个RGBA的序列,因为不便区分。c可以是一个RGB或RGBA二维行数组。

marker:MarkerStyle,表示的是标记的样式,可选,默认’o’。
alpha:标量,0-1之间,可选,默认None。

plt.figure(figsize=(12,8))
plt.title('this is a scatter')

plt.scatter(x[0:10], y[0:10], s=50, c="m", marker='o')
image.png

给C一个序列,画个彩色的气泡图,plt.colorbar()加个颜色标注

plt.figure(figsize=(12,8))
plt.title('This is a scatter')
plt.xlabel('x-value')
plt.ylabel('y-label')
sizes =1000*np.random.rand(100)
colors=np.random.rand(100)

plt.scatter(y, z, s=sizes, c=colors, label='Data 1',alpha=0.3,cmap='spring')
plt.colorbar()

plt.grid()  # 生成网格
plt.show()
image.png

画气泡图,可以用plt.annotate()函数用于标注文字。
s 为注释文本内容
xy 为被注释的坐标点
xytext 为注释文字的坐标位置
xycoords and textcoords 是坐标xy与xytext的说明
weight 设置字体线型
color 设置字体颜色
arrowprops #箭头参数,参数类型为字典dict
bbox给标题增加外框

plt.figure(figsize=(12,8))
plt.title('This is a scatter')
plt.xlabel('x-value')
plt.ylabel('y-label')
sizes =1000*np.random.rand(100)
colors=np.random.rand(100)

plt.scatter(y, z, s=sizes, c=colors, label='Data 1',alpha=0.3,cmap='spring')
plt.colorbar()

plt.annotate(r'this is a point', xy=(y[10], z[10]), xycoords='data', 
             xytext=(+10, +30),textcoords='offset points',
             fontsize=10,arrowprops=dict(arrowstyle='->', 
                                         connectionstyle="arc3,rad=.2"))

plt.grid()  # 生成网格
plt.show()
image.png

相关文章

网友评论

      本文标题:Matplotlib可视化---散点图

      本文链接:https://www.haomeiwen.com/subject/zkwlrdtx.html