Matplotlib data visualization (5)

Table of contents

1. Draw a line chart

2. Draw a scatterplot

3. Draw a histogram

4. Draw a pie chart

5. Draw a boxplot


1. Draw a line chart

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
x = np.arange(9)
y = np.sin(x)
z = np.cos(x)
# marker数据点样式,linewidth线宽,linestyle线型样式,
#color表示颜色
plt.plot(x, y, marker='*', linewidth=1, linestyle='--', color='orange')
plt.plot(x, z)
plt.title('matplotlib')
plt.xlabel('height',fontsize=15)
plt.ylabel('width',fontsize=15)
# 设置图例
plt.legend(['Y','Z'], loc='upper right')
plt.grid(True)
plt.show()

Result graph:

2. Draw a scatterplot

Example 1:

fig,ax = plt.subplots()
plt.rcParams['font.family'] = ['SimHei'] 
plt.rcParams['axes.unicode_minus'] = False 
x1 = np.arange(1,30)
y1 = np.sin(x1)
ax1 = plt.subplot(1,1,1)
plt.title('散点图')
plt.xlabel('X')
plt.ylabel('Y')
lvalue = x1
ax1.scatter(x1,y1,c='c' ,s = 100,linewidths = lvalue,marker = 'o')
plt.legend('x1')
plt.show() 

Result graph:

Example 2:

fig,ax=plt.subplots()
plt.rcParams['font.family']=['SimHei']#用来显示中文标签
plt.rcParams['axes.unicode_minus']=False  #用来正常显示负号
for color in ['red','green','blue']:
    n=500
    x,y=np.random.randn(2,n)    
    ax.scatter(x,y,c=color,label=color,alpha=0.3,edgecolors='none')
    ax.legend()
    ax.grid(True)

plt.show()

 Result graph:

3. Draw a histogram

fig,axes = plt.subplots(2,1)
data = pd.Series(np.random.randn(16),index = list('abcdefghijklmnop'))
data.plot.bar(ax = axes[0],color = 'k',alpha = 0.7) #垂直柱状图 
data.plot.barh(ax = axes[1],color = 'k',alpha = 0.7) #alpha设置透明度 

Result graph:

4. Draw a pie chart

plt.figure(figsize = (6,6))

#建立轴的大小
labels = 'Springs','Summer','Autumn','Winter'
x = [15,30,45,10]
explode = (0.05,0.05,0.05,0.1)
#这个是控制分离的距离的,默认饼图不分离
plt.pie(x,labels = labels,explode = explode,startangle = 60,autopct = '%1.1f%%')
#qutopct在图中显示比例值,注意值的格式
plt.title('Rany days by season')
plt.tick_params(labelsize = 12)
plt.show() 

Result graph:

5. Draw a boxplot

np.random.seed(0)  #设置随机种子
df = pd.DataFrame(np.random.rand(5,4),
columns = ['A', 'B', 'C', 'D'])
#生成0-1之间的5*4维度数据并存入4列DataFrame中
df.boxplot()  #也可用plot.box()
plt.show()

Result graph:


 

Guess you like

Origin blog.csdn.net/m0_64087341/article/details/132367053