1. 程式人生 > 程式設計 >Matplotlib使用字串代替變數繪製散點圖的方法

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使用字串代替變數繪製散點圖的方法

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。