栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

数据可视化--第二次课

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据可视化--第二次课

使用matplotlib绘制图表

步骤:

    导包:import matplotlib.pyplot as plt / import numpy as np准备数据使用函数绘制图像plt.show()

绘图代码:

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(1,8)
y = np.array([1,2,3,4,5,6,7])
plt.plot(x,y)
plt.title("2020080603002")
plt.show()

In [19]:

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(1,13)
y1 = np.array([21,22,23,25,30,33,25,24,26,28,30,33])
y2 = np.array([28,30,31,32,33,33,23,24,33,35,36,20])
plt.plot(x,y1)
plt.plot(x,y2)
plt.title("2020080603002")
plt.show()

In [21]:

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(4)
y = np.array([10,20,30,15])
bar_width = 0.4
plt.bar(x,y,width=bar_width)
plt.title("2020080603002")
plt.show()

In [25]:

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(4)
y = np.array([10,20,30,15])
y1 = np.array([5,10,20,25])
bar_width = 0.4
plt.bar(x,y,width=bar_width)
plt.bar(x+bar_width,y1,width=bar_width)
plt.title("2020080603002")
plt.show()

In [27]:

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(4)
y = np.array([10,20,30,15])
y1 = np.array([5,10,20,25])
bar_width = 0.4
plt.bar(x,y,width=bar_width)
plt.bar(x,y1,width=bar_width,bottom=y)
plt.title("2020080603002")
plt.show()

In [37]:

import matplotlib.pyplot as plt
import numpy as np
y2 = np.arange(5)
x2 = np.array([10,8,7,11,13])
bar_height = 0.3
plt.barh(y2,x2,height=bar_height)
plt.title("2020080603002")
plt.show()

In [41]:

import matplotlib.pyplot as plt
import numpy as np
y2 = np.arange(5)
x2 = np.array([10,8,7,11,15])
x3 = np.array([8,7,6,5,10])
bar_height = 0.3
plt.barh(y2,x2,height=bar_height)
plt.barh(y2+bar_height,x3,height=bar_height)
plt.title("2020080603002")
plt.show()

In [43]:

import matplotlib.pyplot as plt
import numpy as np
y2 = np.arange(5)
x2 = np.array([10,8,7,11,15])
x3 = np.array([8,7,6,5,10])
bar_height = 0.3
plt.barh(y2,x2,height=bar_height)
plt.barh(y2,x3,left=x2,height=bar_height)
plt.title("2020080603002")
plt.show()

import matplotlib.pyplot as plt
import numpy as np
plt.title("2020080603002")
x = np.arange(6)
y1 = np.array([1,3,5,7,9,5])
y2 = np.array([1,4,5,7,10,8])
y3 = np.array([3,2,8,7,6,4])
plt.stackplot(x,y1,y2,y2)
plt.show()

In [2]:

import matplotlib.pyplot as plt
import numpy as np
plt.title("2020080603002")
scores = np.random.randint(0, 100, 51)
plt.hist(scores, bins = 51)
plt.show()

In [18]:

import matplotlib.pyplot as plt
import numpy as np
plt.title("2020080603002")
data = np.array([10,20,31,45,55])
pie_label = (['A', 'B', 'C', 'D', 'E'])
plt.pie(data, radius = 1.5, labels = pie_label, autopct = '%3.2f%%')
plt.show()

In [20]:

import matplotlib.pyplot as plt
import numpy as np
plt.title("2020080603002")
data = np.array([10,20,35,45,55])
pie_label = (['A', 'B', 'C', 'D', 'E'])
pie_explode = [0.1, 0.1, 0.1, 0.1, 0.1]
plt.pie(data, radius = 1.5, labels = pie_label, autopct = '%3.2f%%', explode = pie_explode, shadow = True)
plt.show()

In [21]:

import matplotlib.pyplot as plt
import numpy as np
plt.title("2020080603002")
data = np.array([10,20,31,45,55])
pie_label = (['A', 'B', 'C', 'D', 'E'])
plt.pie(data, radius = 1.5, labels = pie_label, autopct = '%3.2f%%', wedgeprops = {'width': 0.7}, pctdistance = 0.8)
plt.show()

In [22]:

import matplotlib.pyplot as plt
import numpy as np
plt.title("2020080603002")
x = np.random.rand(50)
y = np.random.rand(50)
area = x * 30 + y * 30
plt.scatter(x,y, s = area)
plt.show()

In [26]:

import matplotlib.pyplot as plt
import numpy as np
plt.title("2020080603002")
data = np.random.rand(100)
plt.boxplot(data, meanline = True, widths = 0.4,vert = False )
plt.show()

In [34]:

import matplotlib.pyplot as plt
import numpy as np
plt.title("2020080603002")
plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False
num = 3
data = np.array([[0.5, 0.6, 0.7],
               [0.4, 0.45, 0.8],
               [0.2, 0.65, 0.5]])
angles = np.linspace(0, 2 * np.pi, num, endpoint = False)
angles = np.concatenate((angles, [angles[0]]))
data = np.concatenate((data, [data[0]]))
polar_lable = ['艺术', '逻辑', '想象']
polar_lable = np.concatenate((polar_lable, [polar_lable[0]]))
plt.polar(angles, data)
plt.thetagrids(angles * 180/np.pi, labels = polar_lable)
plt.fill(angles, data, alpha = 0.25)
plt.show()
C:UsersADMINI~1AppDataLocalTemp/ipykernel_7760/3312525444.py:15: UserWarning: Trying to create polar plot on an Axes that does not have a polar projection.
  plt.polar(angles, data)

In [40]:

import matplotlib.pyplot as plt
import numpy as np
plt.title("2020080603002")
x = np.arange(5)
y = np.array([20, 10, 5, 15, 30])
error = [3, 5, 2, 3, 3]
plt.errorbar(x, y, yerr = error, capsize = 3, capthick = 2)
plt.show()

In [6]:

import matplotlib.pyplot as plt
import numpy as np
plt.title("2020080603002")
plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False
x = np.arange(3)
y1 = ([2.04, 1.57, 1.63])
y2 = ([1.69, 1.61, 1.64])
y3 = ([4.65, 4.99, 4.94])
y4 = ([3.39, 2.33, 4.10])
error1 = [0.16,0.08,0.10]
error2 = [0.27,0.14,0.14]
error3 = [0.34,0.32,0.29]
error4 = [0.23,0.23,0.39]
bar_width = 0.3
plt.bar(x, y1, bar_width)
plt.bar(x + bar_width, y2, bar_width, align = 'center', tick_label =["春季", "夏季", "秋季"])
plt.bar(x + 2*bar_width, y3, bar_width)
plt.bar(x + 3*bar_width, y4, bar_width)
plt.errorbar(x, y1, yerr = error1, capsize = 3, elinewidth = 2, fmt = 'k,' )
plt.errorbar(x + bar_width, y2,  yerr = error2, capsize = 3, elinewidth = 2, fmt = 'k,' )
plt.errorbar(x + 2*bar_width, y3,  yerr = error3, capsize = 3, elinewidth = 2, fmt = 'k,' )
plt.errorbar(x + 3*bar_width, y4,  yerr = error4, capsize = 3, elinewidth = 2, fmt = 'k,' )
plt.show()

In [ ]:

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/755030.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号