import matplotlib
import matplotlib.pyplot as plt
from matplotlib import font_manager
import numpy as np
import random
#資料
x = [i+1 for i in range(12)]
y_2020 = [random.randint(20,36) for i in range(12)]
y_2021 = [random.randint(20,36) for i in range(12)]
y_2022 = [random.randint(20,36) for i in range(12)]
x_ = ["{}月".format(i+1) for i in range(12)]
#顯示中文字型
font = {
'style' : "italic" ,
'variant':"small-caps" ,
'weight' : "50",
'stretch': "extra-condensed",
'size' : "20.0",
'fname' :"C:\Windows\Fonts\simfang.ttf" ,
}
my_font = font_manager.FontProperties("font",**font)
#設定繪圖視窗大小,背景顔色,框顔色等
plt.figure(num="123",figsize=(15,8),dpi=100,facecolor="Green",edgecolor="red",linewidth=20,frameon=True)
#設定x軸和y軸的步長
plt.xticks(x,x_,rotation=30,fontproperties=my_font) #rotation=90 旋轉90度
plt.yticks(np.arange(20,37,1))
#設定x軸和y軸的表示資訊
# plt.xlabel("月份",size=120,color="red")
plt.xlabel("月份",fontproperties=my_font,color="blue")
plt.ylabel("銷售額",fontproperties=my_font)
#設定标題
plt.title("2020-2022年銷售額",fontproperties=my_font)
#繪制網格
plt.grid(alpha=2,which='both', axis='both',color="red",linestyle="-.",linewidth=1.0)
#繪制折線圖
plt.plot(x, # X軸資料
y_2020, # Y軸資料
label="2020年", #折線的标簽名稱
ls="-", # 線條樣式
lw=2, # 線條寬度
marker="o", # X軸Y軸對應資料點的樣式
ms=20, # 資料點的大小
mfc="orange", # 資料點填充顔色
alpha=0.6) # 資料點的透明度
plt.plot(x, # X軸資料
y_2021, # Y軸資料
label="2021年", #折線的标簽名稱
ls="--", # 線條樣式
lw=2, # 線條寬度
marker="o", # X軸Y軸對應資料點的樣式
ms=20, # 資料點的大小
mfc="#008B45", # 資料點填充顔色
alpha=0.6) # 資料點的透明度
plt.plot(x, # X軸資料
y_2022, # Y軸資料
label="2021年", #折線的标簽名稱
ls="-.", # 線條樣式
lw=2, # 線條寬度
marker="o", # X軸Y軸對應資料點的樣式
ms=20, # 資料點的大小
mfc="#6495ED", # 資料點填充顔色
alpha=0.6) # 資料點的透明度
# plt.plot(x,y_2020,label="2020年",color="#008B45",linestyle="-",linewidth=3,alpha=10)
# plt.plot(x,y_2021,label="2021年",color="Black",linestyle="--",linewidth=3)
# plt.plot(x,y_2022,label="2022年",color="#6495ED",linestyle="--",linewidth=3)
#添加圖例 得添加在plt.plot()後
plt.legend(prop=my_font,loc=1)
#添加水印
plt.text(x=8, # 水印開頭左下角對應的X點
y=20, # 水印開頭左下角對應的Y點
s="Matplotlib", # 水印文本
fontsize=50, # 水印大小
color="blue", # 水印顔色
alpha=0.5) # 水印是通過透明度控制的
#儲存圖檔
plt.savefig(r"C:\Users\jinag\Pictures\Saved Pictures\12.png")
plt.show()
plt.plot(
x, # X軸資料
y_2021, # Y軸資料
label="2020年", #折線的标簽名稱
ls="-", # 線條樣式 - -. --
lw=2, # 線條寬度
marker="o", # X軸Y軸對應資料點的樣式
ms=20, # 資料點的大小
mfc="orange", # 資料點填充顔色
alpha=0.6) # 資料點的透明度
具體解析
'markers': {'.': 'point', ',': 'pixel', 'o': 'circle', 'v': 'triangle_down', '^': 'triangle_up', '<': 'triangle_left', '>': 'triangle_right', '1': 'tri_down', '2': 'tri_up', '3': 'tri_left', '4': 'tri_right', '8': 'octagon', 's': 'square', 'p': 'pentagon', '*': 'star', 'h': 'hexagon1', 'H': 'hexagon2', '+': 'plus', 'x': 'x', 'D': 'diamond', 'd': 'thin_diamond', '|': 'vline', '_': 'hline', 'P': 'plus_filled', 'X': 'x_filled', 0: 'tickleft', 1: 'tickright', 2: 'tickup', 3: 'tickdown', 4: 'caretleft', 5: 'caretright', 6: 'caretup', 7: 'caretdown', 8: 'caretleftbase', 9: 'caretrightbase', 10: 'caretupbase', 11: 'caretdownbase', 'None': 'nothing', None: 'nothing', ' ': 'nothing', '': 'nothing'},
參數 | 解析(全稱) |
---|---|
. | point |
, | pixel |
o | circle |
v | triangle_down |
^ | triangle_up |
< | triangle_left' |
> | triangle_right |
'1' | tri_down |
'2' | tri_up |
'3' | tri_left |
'4' | tri_right |
'8' | octagon |
s | square |
p | pentagon |
* | star |
h | hexagon1 |
H | hexagon2 |
+ | plus |
x | x |
D | diamond |
d | thin_diamond |
| | vline |
_ | hline |
P | plus_filled |
X | x_filled |
tickleft | |
1 | tickright |
2 | tickup |
3 | tickdown |
4 | caretleft |
5 | caretright |
6 | caretup |
7 | caretdown |
8 | caretleftbase |
9 | caretrightbase |
10 | caretupbase |
11 | caretdownbase |
None | nothing |
'None' | nothing |
'' | nothing |
' ' | nothing |