Matplotlib data visualization (6)

Table of contents

1. Draw a probability map

2. Draw a radar chart

3. Draw a flow diagram

4. Draw Polar Coordinate Plot

5. Draw a word cloud


1. Draw a probability map

from scipy.stats import norm
fig,ax = plt.subplots()
plt.rcParams['font.family'] = ['SimHei'] 
np.random.seed()
mu = 100
sigma = 15
x = mu+sigma*np.random.randn(437)
num_bins = 50
n,bins,patches = ax.hist(x,num_bins,density = 1,color='c')
y=norm.pdf(bins,mu,sigma)
ax.plot(bins,y,'r--')
fig.tight_layout()
plt.show()

Result graph:

2. Draw a radar chart

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
# 某学生的课程与成绩
courses = ['数据结构', '数据可视化', '高数', '英语', '软件工程', '组成原理', 'C语言', '体育']
scores = [82, 95, 78, 85, 45, 88, 76, 88]
dataLength = len(scores)               # 数据长度
# angles数组把圆周等分为dataLength份
angles = np.linspace(0, 2*np.pi, dataLength, endpoint=False)  
courses.append(courses[0])
scores.append(scores[0])
angles = np.append(angles,angles[0])  # 闭合
# 绘制雷达图
plt.polar(angles,              # 设置角度
          scores,            # 设置各角度上的数据
          'bv--',             # 设置颜色、线型和端点符号
          linewidth=2)       # 设置线宽
# 设置角度网格标签
plt.thetagrids(angles*180/np.pi, courses, fontproperties='simhei', fontsize=12)
# 填充雷达图内部
plt.fill(angles, scores, facecolor='g', alpha=0.2)
plt.show()

Result graph:

3. Draw a flow diagram

import numpy as np
import matplotlib.pyplot as plt

Y, X = np.mgrid[-3:3:100j, -3:3:100j]
U = -1 - X**2 + Y
V = 1 + X - Y**2
speed = np.sqrt(U*U + V*V)

plt.streamplot(X, Y, U, V, color=U, linewidth=2, cmap=plt.cm.autumn)
plt.colorbar()

f, (ax1, ax2) = plt.subplots(ncols=2)
ax1.streamplot(X, Y, U, V, density=[0.5, 1])

lw = 5*speed/speed.max()
ax2.streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw)

plt.show()

Result graph:

4. Draw Polar Coordinate Plot

r = np.linspace(0, 2, 100)
theta = 2 * np.pi * r
fig = plt.figure(figsize=(13, 4))
ax1 = plt.subplot(121, projection='polar')
ax1.scatter(theta, r, label = 'Polar Projection', s = 10)
ax1.legend(bbox_to_anchor = (0.85, 1.35))
ax2 = plt.subplot(122)
ax2.scatter(theta, r, label = 'Planar Projection', s = 5)
ax2.legend(bbox_to_anchor = (0.85, 1.35))
ax2.set_xlabel('R')
ax2.set_ylabel(r'$\theta$')

Result graph:

 

5. Draw a word cloud

import matplotlib.pyplot as plt
from wordcloud import WordCloud,STOPWORDS
# from pyecharts.charts import WordCloud
import PIL.Image as image
import numpy as np
def get_wordList():
    f = open("data//text.txt")
    wordList = f.read()
    return wordList
def get_wordClound(mylist):
    pic_path = 'data//myimg.jpg'
    img_mask = np.array(image.open(pic_path))
    wordcloud = WordCloud(background_color="white",mask=img_mask).generate(mylist)
    plt.imshow(wordcloud)
    plt.axis("off")
     
wordList = get_wordList()
get_wordClound(wordList)

Result graph:

 

Guess you like

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