Python Visualization - Bubble Chart

The bubble chart is similar to the scatter chart, which also represents the changing relationship between the XY axis coordinates, and can also color the points like a color scatter chart.

The difference is that you can intuitively feel the size of the numerical value represented by the size of the scatter in the figure.

1. Data file preparation

1、PeopleNumber.csv

city,people,price
NJ ,823,3.19
XZ ,866,2.7
HA,487,2.51
YC,723,2.78
SQ,485,2.61
TZ,464,3.13
YZ,448,3.14
NT,730,2.69
LYG, 447.2.51
ZJ, 318,3.15
WX,651,3.15
SZ,1061,3.15
CZ,470,3.15

The X-axis and Y-axis of the bubble chart depict the relationship between population and prices, respectively, and different cities are marked with different colors.

The first column is the city (abbreviation) index, the second column is the population value, and the third column is the price value.

The csv file is essentially a txt file separated by commas (,), so it is opened in excel as:

2. Import the module package

You can refer to the installation of Python and matplotlib packages under Windows and related
https://blog.csdn.net/mikasa3/article/details/78942650 

1、numpy

2、pandas

3、seaborn

4 、 matplotlib

3. Complete code

as follows:

import numpy as np  
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
def DrawBubble(read_name):#Bubble Chart
    sns.set(style = "whitegrid")#Set the style
    fp = pd.read_csv(read_name)#Data source
    x = fp.people#X axis data
    y = fp.price#Y-axis data
    z = fp.price# is used to adjust the size of each point s
    cm = plt.cm.get_cmap('RdYlBu')
    fig,ax = plt.subplots(figsize = (12,10))
    #Pay attention to the method of s discretization, because it is necessary to intuitively feel the numerical value represented by the size of the point
    #What I use is the value of the current point minus the minimum value in the set +0.1 and then *1000
    #Parameters are X-axis data, Y-axis data, the size of each point, the color of each point
    bubble = ax.scatter(x, y , s = (z - np.min(z) + 0.1) * 1000, c = z, cmap = cm, linewidth = 0.5, alpha = 0.5)
    ax.grid()
    fig.colorbar(bubble)
    ax.set_xlabel('people of cities', fontsize = 15)#X axis label
    ax.set_ylabel('price of something', fontsize = 15)#Y轴标签
    plt.show()
if __name__=='__main__':
    DrawBubble("PeopleNumber.csv")#Bubble Chart

4. Operation results

1. Bubble chart


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324758934&siteId=291194637