网络编程
位置:首页>> 网络编程>> Python编程>> Matplotlib使用字符串代替变量绘制散点图的方法

Matplotlib使用字符串代替变量绘制散点图的方法

作者:进阶的JFarmer  发布时间:2021-04-18 06:10:11 

标签:Matplotlib,散点图

要点说明

在绘制散点图的时候,通常使用变量作为输入数据的载体。
其实,也可以使用字符串作为输入数据的存储载体。

下面代码的data = {“a”: x, “b”: y, “color”: c, “size”: s}正是将散点图的输入数据、颜色和标记大小放在数据字典data中作为键值对,对应的key是字符串string。

Matplotlib编程实现


import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.gca()

x = np.random.rand(50)*10
y = np.random.rand(50)*10+20
s = np.random.rand(50)*100
c = np.random.rand(50)

data = {"a": x, "b": y, "color": c, "size": s}

ax.scatter("a", "b", c="color", s="size", data=data)

ax.set(xlabel="X", ylabel="Y")

plt.show()

成品图

Matplotlib使用字符串代替变量绘制散点图的方法

来源:https://blog.csdn.net/weixin_43896318/article/details/104335581

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com