天天看點

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基礎圖形的繪制

②:散點圖

顯示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基礎圖形的繪制

③:條形圖

電影院的總票價

橫着的條形圖

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基礎圖形的繪制

④:直方圖

把大量的資料按照要求分組,然後繪圖

組數=極差/組距(盡量要求整除)

繪制方法: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()