Python基础图形的绘制

Python基本图形的绘制

①:折线图

显示三个月每天的天气:

from matplotlib import pyplot as plt
import random
from matplotlib.font_manager import FontProperties
#windows、macOS和linux设置字体的方式
myfont = FontProperties(fname=r"C:\Windows\Fonts\STFANGSO.TTF")
x = range(0, 120)
y = [random.randint(20, 35) for i in range(120)]
plt.figure(figsize=(20, 8), dpi=80)
plt.plot(x, y)
_xtick_labels = ["10点{}分".format(i) for i in range(60)]
_xtick_labels += ["11点{}分".format(i) for i in range(60)]
#取步长,用字符串替换数字,保证步长一致
plt.xticks(list(x)[::3], _xtick_lables[::3], rotation=45,fontproperties=myfont)
#rotation是调整旋转的角度
#fontproperties=myfont是选择字体的格式
plt.xlabel("时间", fontproperties=myfont)
plt.ylabel("温度", fontproperties=myfont)
plt.title("10点到12点温度随时间的变化", fontproperties=myfont)
plt.show()

Python基础图形的绘制_第1张图片

②:散点图

显示3月和10月的天气变化:

from matplotlib import pyplot as plt
from matplotlib.font_manager import FontProperties
my_font = FontProperties(fname=r"C:\Windows\Fonts\STFANGSO.TTF")
y_3 = [11, 12, 20, 14, 6, 8, 12, 20, 19, 11, 12, 3, 14, 6, 8, 12, 20, 19, 11, 12, 3, 14, 6, 8, 12, 20, 19, 11, 12, 3, 14]
y_10 = [20,21,23,19,21,20,20,20,21,23,19,21,20,20,20,21,23,19,21,20,20,20,21,23,19,21,20,20,20,21,23]

x_3 = range(1, 32)
x_10 = range(51, 82)
plt.figure(figsize=(20, 8), dpi=80)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190710104118145.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzNDk0OTk3,size_16,color_FFFFFF,t_70)
plt.scatter(x_3, y_3, label="3月份")
plt.scatter(x_10, y_10, label="10月份")

_x = list(x_3)+list(x_10)
_xtick_labels = ["3月{}日".format(i) for i in x_3]
_xtick_labels += ["10月{}日".format(i-50) for i in x_10]
plt.xticks(_x[::3], _xtick_labels[::3], fontproperties=my_font, rotation=45)
plt.legend(loc="upper left", prop=my_font)
plt.xlabel("时间", fontproperties=my_font)
plt.ylabel("温度", fontproperties=my_font)
plt.show()

Python基础图形的绘制_第2张图片

③:条形图

电影院的总票价

横着的条形图

from matplotlib import pyplot as plt
from matplotlib.font_manager import FontProperties
my_font = FontProperties(fname=r"C:\Windows\Fonts\STFANGSO.TTF")

a = ["战狼2", "速度与激情8", "功夫瑜伽", "西游伏魔篇", "变形金刚5:最后的骑士", "摔跤吧!爸爸", "加勒比海盗5:死无对证", "金刚:骷髅岛", "生化危机6:终章", "蜘蛛侠:英雄归来"]
b = [56.1, 17.53, 11.28, 10.49, 32, 6.99, 6.23, 5.55, 14.23, 11.12]

print(len(a))
print(len(b))

plt.figure(figsize=(20, 8), dpi=80)
#绘制条形图
plt.barh(range(len(a)), b, height=0.3, color="orange")
#设置字符串到y轴
plt.yticks(range(len(a)), a, fontproperties=my_font)
#设置表格,以及其透明度
plt.grid(alpha=0.3)

plt.show()

竖着的条形图

from matplotlib import pyplot as plt
from matplotlib.font_manager import FontProperties
my_font = FontProperties(fname=r"C:\Windows\Fonts\STFANGSO.TTF")

a = ["战狼2", "速度与激情8", "功夫瑜伽", "西游伏魔篇", "变形金刚5:最后的骑士", "摔跤吧!爸爸", "加勒比海盗5:死无对证", "金刚:骷髅岛", "生化危机6:终章", "蜘蛛侠:英雄归来"]
b = [56.1, 17.53, 11.28, 10.49, 32, 6.99, 6.23, 5.55, 14.23, 11.12]

print(len(a))
print(len(b))

plt.figure(figsize=(20, 8), dpi=80)
#绘制条形图
plt.bar(range(len(a)), b, width=0.3, color="orange")
#设置字符串到x轴
plt.xticks(range(len(a)), a, fontproperties=my_font, rotation=90)
#设置表格,以及其透明度
plt.grid(alpha=0.3)

plt.show()

横着的条形图:
方法:plt.barh()
设置宽度:heigth=
竖着的条形图:
方法:plt.bar()
设置宽度:width=

电影连续几天的票房条形图:

# -*- coding:utf-8 -*-
#@Time  : 2019/7/10 13:01
from matplotlib import pyplot as plt
from matplotlib.font_manager import FontProperties
my_font = FontProperties(fname=r"C:\Windows\Fonts\STFANGSO.TTF")

a=["猩球崛起3:终极之战", "肯林可乐", "蜘蛛侠:英雄归来", "战狼2"]

b_1 = [12333, 312, 4499, 132]
b_2 = [12333, 156, 2045, 168]
b_3 = [2358, 399, 2358, 368]
#将几天的票房分隔开让条形图之间不互相覆盖
x_1 = list(range(len(a)))
x_2 = [i+0.3 for i in x_1]
x_3 = [i+0.3*2 for i in x_1]

plt.figure(figsize=(20,8), dpi=80)
plt.bar(range(len(a)), b_1, width=0.3, label="第一天")
plt.bar(x_2, b_2, width=0.3, label="第二天")
plt.bar(x_3, b_3, width=0.3, label="第三天")
#设置图例样式
plt.legend(prop=my_font)

plt.xticks(x_2, a, fontproperties=my_font)

plt.show()

Python基础图形的绘制_第3张图片

④:直方图

把大量的数据按照要求分组,然后绘图
组数=极差/组距(尽量要求整除)
绘制方法:hist(要求操作的数据, 组数)

电影时长的直方图:

from matplotlib import pyplot as plt
a = [120, 123, 94, 120, 110, 95, 96, 97, 106, 103, 104, 109, 112, 115, 117, 119, 115, 130, 135, 136, 120, 123, 125, 120, 110, 95, 96, 97, 106, 103, 104, 109, 112, 115, 117, 119, 115, 130, 135, 136, 120, 123, 125, 120, 110, 95, 96, 97, 106, 103, 104, 109, 112, 115, 117, 119, 115, 130, 135, 136, 120, 123, 125, 120, 110, 95, 96, 97, 106, 103, 104, 109, 112, 115, 117, 119, 115, 130, 135, 136, 120, 123, 125, 120, 110, 95, 96, 97, 106, 103, 104, 109, 112, 115, 117, 119, 115, 130, 135, 136, 120, 123, 125, 120, 110, 95, 96, 97, 106, 103, 104, 109, 112, 115, 117, 119, 115, 130, 135, 136, 120, 123, 125, 120, 110, 95, 96, 97, 106, 103, 104, 109, 112, 115, 117, 119, 115, 130, 135, 136, 120, 123, 125, 120, 110, 95, 96, 97, 106, 103, 104, 109, 112, 115, 117, 119, 115, 130, 135, 136, 120, 123, 125, 120, 110, 95, 96, 97, 106, 103, 104, 109, 112, 115, 117, 119, 115, 130, 135, 136, 120, 123, 125, 120, 110, 95, 96, 97, 106, 103, 104, 109, 112, 115, 117, 119, 115, 130, 135, 136, 120, 123, 125, 120, 110, 95, 96, 97, 106, 103, 104, 109, 112, 115, 117, 119, 115, 130, 135, 136, 120, 123, 125, 120, 110, 95, 96, 97, 106, 103, 104, 109, 112, 115, 117, 119, 115, 130, 135, 136, 116, 122, 125, 126, 133, 99, 95, 106, 109, 112, 129, 139, 122]

#设置组距
d = 5
#用极差整除组距得到组数
num_bin = (max(a)-min(a))//d

plt.figure(figsize=(20, 8), dpi=80)

plt.hist(a, num_bin)

plt.xticks(list(range(min(a), max(a)+d))[::d])

plt.grid()
plt.show()

你可能感兴趣的:(python学习)