天天看點

python 散點圖_Python繪制散點圖

python 散點圖_Python繪制散點圖

import numpy as np

import pandas as pd

import matplotlib as mpl

import matplotlib.pyplot as plt

# 版本檢查

print(mpl.__version__) #> 3.0.0

# 導入資料集

midwest = pd.read_csv("https://raw.githubusercontent.com/selva86/datasets/master/midwest_filter.csv")

# midwest['category']分類,顔色設定為與其一樣多

categories = np.unique(midwest['category'])

colors = [plt.cm.tab10(i/float(len(categories)-1)) for i in range(len(categories))]

# 每個分類plot

plt.figure(figsize=(16, 10), dpi= 80, facecolor='w', edgecolor='k')

for i, category in enumerate(categories):

plt.scatter('area', 'poptotal',

data=midwest.loc[midwest.category==category, :],

s=20, c=colors[i], label=str(category))

# 修改x軸,y軸坐标系尺寸區間

plt.gca().set(xlim=(0.0, 0.1), ylim=(0, 90000),

xlabel='Area', ylabel='Population')

plt.xticks(fontsize=12)

plt.yticks(fontsize=12)

plt.title("Midwest Area vs Population", fontsize=22)

plt.legend(fontsize=12)

plt.show()

本案例在Github上的開源連結為:

jackzhenguo/python-small-examples​github.com

python 散點圖_Python繪制散點圖

歡迎關注公衆号【Python與算法社群】,點選下方連結,一鍵擷取273篇原創學習筆記

認真就能打動人:273篇幹貨資料彙總​mp.weixin.qq.com

python 散點圖_Python繪制散點圖