数据可视化图表-散点图(Scatter plot)

本文是接着这篇内容的:https://blog.csdn.net/Cassiel60/article/details/88350442

散点图是用于研究两个变量之间关系的经典的和基本的图表。 如果数据中有多个组,则可能需要以不同颜色可视化每个组。 在 matplotlib 中,您可以使用 plt.scatterplot() 方便地执行此操作

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

# prepare data
#create as many colors as there are unique midwest['category']
categories = np.unique(midwest['category'])
colors = [plt.cm.tab10(i/float(len(categories)-1)) for i in range(len(categories))]

# draw plot for each category
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))

#decorations
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("Scatterplot of Midwest Area vs Population", fontsize=22)
plt.legend(fontsize = 12)
plt.show()

数据可视化图表-散点图(Scatter plot)_第1张图片

你可能感兴趣的:(python)