欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

matplotlib繪制折線圖的基本配置(萬(wàn)能模板案例)

 更新時(shí)間:2022年04月13日 10:45:20   作者:王小王-123  
折線圖可以很方便的看出數(shù)據(jù)的對(duì)比,本文主要介紹了matplotlib繪制折線圖的基本配置(萬(wàn)能模板案例),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前面我們已經(jīng)構(gòu)造了一種圖形可視化的模板了,下面我們直接使用這個(gè)模板進(jìn)行增添和修改,進(jìn)一步的改善圖形的外觀。

import matplotlib.pyplot as plt
 
# 畫布
plt.figure(figsize=(9,3),   # (寬度 , 高度) 單位inch 
           dpi=100,         #  清晰度 dot-per-inch
           facecolor='#CCCCCC', # 畫布底色
           edgecolor='black',linewidth=0.2,frameon=True, # 畫布邊框
           #frameon=False  # 不要畫布邊框
          )         
 
# ax = plt.gca()
# ax.plot()
 
plt.plot()
plt.show()

設(shè)置好基本的圖形之后,我們就可以向上面添加一些數(shù)據(jù)了

(圖例放置位置)

"""legend( handles=(line1, line2, line3),
           labels=('label1', 'label2', 'label3'),
           'upper right')
    The *loc* location codes are::
          'best' : 0,          (currently not supported for figure legends)
          'upper right'  : 1,
          'upper left'   : 2,
          'lower left'   : 3,
          'lower right'  : 4,
          'right'        : 5,
          'center left'  : 6,
          'center right' : 7,
          'lower center' : 8,
          'upper center' : 9,
          'center'       : 10,"""

折線圖案例

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
 
# 畫布
plt.figure(figsize=(15,5),   # (寬度 , 高度) 單位inch 
           dpi=100,         #  清晰度 dot-per-inch
           facecolor='#CCCCCC', # 畫布底色
           edgecolor='black',linewidth=0.2,frameon=True, # 畫布邊框
           #frameon=False  # 不要畫布邊框
          )         
 
 
# 數(shù)據(jù)
x = np.linspace(0, 2 * np.pi, 50)  
y1 = np.sin(x)
y2 = np.cos(x)
df = pd.DataFrame([x,y1,y2]).T
df.columns = ['x','sin(x)','cos(x)']
 
# 圖形
plt.plot(df['x'],df['sin(x)'],label='sin(x)')
plt.plot(df['x'],df['cos(x)'],label='cos(x)')
 
# 圖例
plt.legend(loc='lower right')  # 不帶參數(shù)的時(shí)候,使用圖形的label屬性
# plt.legend(labels=['sin','cos'])
 
# 標(biāo)題
#plt.title("sin(x) and cos(x)",loc='center',y=0.85)
 
# 字體字典
font_dict = {'fontsize': 12, 'fontweight': 'bold', 'color': 'green'}
plt.title("sin(x) and cos(x)",loc='center',y=0.9, fontdict=font_dict)

查看全局參數(shù)

# matplotlib.pyplot的全局參數(shù)
plt.rcParams
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
 
# 設(shè)置中文字體
plt.rcParams['axes.unicode_minus'] = False    # 不使用中文減號(hào)
plt.rcParams['font.sans-serif'] = 'FangSong'  # 設(shè)置字體為仿宋(FangSong)
# 畫布
plt.figure(figsize=(15,5),   # (寬度 , 高度) 單位inch 
           dpi=120,         #  清晰度 dot-per-inch
           facecolor='#CCCCCC', # 畫布底色
           edgecolor='black',linewidth=0.2,frameon=True, # 畫布邊框
           #frameon=False  # 不要畫布邊框
          )         
 
 
# 數(shù)據(jù)
x = np.linspace(0, 2 * np.pi, 50)  
y1 = np.sin(x)
y2 = np.cos(x)
df = pd.DataFrame([x,y1,y2]).T
df.columns = ['x','sin(x)','cos(x)']
 
# 圖形
plt.plot(df['x'],df['sin(x)'],label='sin(x)')
plt.plot(df['x'],df['cos(x)'],label='cos(x)')
 
# 圖例
plt.legend()
 
# 標(biāo)題
#plt.title("sin(x) and cos(x)",loc='center',y=0.85)
 
# 字體字典
font_dict = {'fontsize': 10, 'fontweight': 'bold', 'color': 'grey'}
 
# 中文標(biāo)題, 默認(rèn)的字體不支持中文
plt.title("三角函數(shù):正弦和余弦",loc='center',y=0.9, fontdict=font_dict)

 改變字體

# 字體字典
font_dict = {'fontsize': 10, 'fontweight': 'bold', 'color': 'grey'}
 
# 中文標(biāo)題, 默認(rèn)的字體不支持中文
plt.title("三角函數(shù):正弦和余弦",loc='center',y=0.9, fontdict=font_dict)

添加X軸和Y軸

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
 
# 畫布
plt.figure(figsize=(15,5),   # (寬度 , 高度) 單位inch 
           dpi=120,         #  清晰度 dot-per-inch
           facecolor='#CCCCCC', # 畫布底色
           edgecolor='black',linewidth=0.2,frameon=True, # 畫布邊框
           #frameon=False  # 不要畫布邊框
          )         
 
 
# 數(shù)據(jù)
x = np.linspace(0, 2 * np.pi, 50)  # 
y1 = np.sin(x)
y2 = np.cos(x)
df = pd.DataFrame([x,y1,y2]).T
df.columns = ['x','sin(x)','cos(x)']
 
 
 
 
 
# 標(biāo)題
#plt.title("sin(x) and cos(x)",loc='center',y=0.85)
 
# 字體字典
font_dict = {'fontsize': 10, 'fontweight': 'bold', 'color': 'black','fontfamily':'KaiTi'}
 
# 設(shè)置全局中文字體
plt.rcParams['font.sans-serif'] = 'KaiTi' # 設(shè)置全局字體為中文 楷體
plt.rcParams['axes.unicode_minus'] = False # 不使用中文減號(hào)
 
# 常用中文字體
# 宋體 SimSun
# 黑體 SimHei
# 微軟雅黑 Microsoft YaHei
# 微軟正黑體 Microsoft JhengHei
# 新宋體 NSimSun
# 新細(xì)明體 PMingLiU
# 細(xì)明體 MingLiU
# 標(biāo)楷體 DFKai-SB
# 仿宋 FangSong
# 楷體 KaiTi
 
# 中文標(biāo)題, 默認(rèn)的字體不支持中文
plt.title("三角函數(shù):正弦和余弦",loc='center',y=1, fontdict=font_dict)
 
# Axes 坐標(biāo)系設(shè)置
ax = plt.gca()  # 獲取當(dāng)前坐標(biāo)系
ax.set_facecolor('#FEFEFE')  # 設(shè)置坐標(biāo)系參數(shù)。。。。
#plt.xlabel()  =>  ax.set_xlabel()
# ax.set_facecolor('#EE2211')
# ax.set_alpha(0.15)
# plt.title() => ax.set_title("AX TITLE")  
 
 
# X軸標(biāo)簽
plt.xlabel("X軸")  # loc: 左中右 left-center-right
# Y軸標(biāo)簽
plt.ylabel("Y軸")   # loc: 上中下 top-center-bottom
 
# X軸范圍
plt.xlim(0,np.pi)  # 只顯示X在0-Pi之間的部分
# Y軸范圍
plt.ylim([0,1.1])  # 只顯示Y在0-1之間的部分
 
# X軸刻度
xticks = np.array([0,1/4,2/4,3/4,1]) * np.pi      # X 軸上刻度的值
labels = ["0",'1/4 Π','1/2 Π','3/4 Π', 'Π']  # X 軸上刻度標(biāo)簽
plt.xticks(xticks, labels)   # 如果沒有傳入labels,直接使用ticks作為labels
# Y軸刻度
yticks = np.arange(0.0,1.2,0.2)     # X 軸上刻度的值
plt.yticks(yticks)   # 如果沒有傳入labels,直接使用ticks作為labels
 
# 根據(jù)刻度畫網(wǎng)格線
#plt.grid()
plt.grid(axis='x')  # axis: both, x, y 在哪個(gè)軸上畫格子
 
# 圖形
plt.plot(df['x'],df['sin(x)'],label='sin(x)')
plt.plot(df['x'],df['cos(x)'],label='cos(x)')
 
# 圖例
plt.legend()
# plt.legend(labels=['sin','cos'])

 折線圖繪制萬(wàn)能模板

# 處理數(shù)據(jù)
df = pd.read_csv(r'unemployment-rate-1948-2010.csv',usecols=['Year','Period','Value'])
df.replace('^M','-',regex=True, inplace=True)
df['year_month'] = df['Year'].astype('U') + df['Period']
 
# 設(shè)置畫布和參數(shù)
plt.figure(figsize=(16,4), dpi=130, facecolor='white', edgecolor='black', frameon=True)# 畫布底色
 
 
# 添加數(shù)據(jù)
plt.plot(df['year_month'], df['Value'],'c')#改變顏色和線條
 
'''
一般不需要改動(dòng)下面的,只需要設(shè)置一些固定常量
'''
 
# 構(gòu)造X軸標(biāo)簽,一般不用設(shè)置
xticks = [df['year_month'][i] for i in np.arange(0,df['year_month'].size,15)]#X軸的顯示
#X軸設(shè)置傾斜度,可以解決標(biāo)簽過長(zhǎng)的問題,大小可以設(shè)置默認(rèn)
plt.xticks(xticks,rotation=100,size=10)
 
# 設(shè)置圖形上的各類主題值
plt.suptitle('主標(biāo)題:unemployment-rate-1948-2010',size=17,y=1.0)
plt.title("繪制日期:2022年   昵稱:王小王-123", loc='right',size=15,y=1)
 
plt.title("主頁(yè):https://blog.csdn.net/weixin_47723732", loc='left',size=12,y=1)
 
# 設(shè)置坐標(biāo)軸上的字體標(biāo)簽
font_dict = {'fontsize': 15, 'fontweight': 'bold', 'color': 'black','fontfamily':'KaiTi'}
plt.xlabel('年月',font_dict)
plt.ylabel('失業(yè)率',font_dict)

到此這篇關(guān)于matplotlib繪制折線圖的基本配置(萬(wàn)能模板案例)的文章就介紹到這了,更多相關(guān)matplotlib繪制折線圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論