matplotlib畫圖xticks設置為字母


matplotlib畫圖xticks設置為字母

一、總結

一句話總結:

plt.xticks(np.arange(6), ('','t1', 't2', 't3', 't4', 't5'))
很多時候都可以去看幫助文檔,里面介紹的非常詳細

 

 

二、matplotlib畫圖xticks設置為字母

博客對應課程的視頻位置:

 

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import make_interp_spline
# 設置matplotlib庫字體的非襯線字體為黑體
plt.rcParams["font.sans-serif"]=["SimHei"]
# 設置matplotlib庫字體族為非襯線字體
plt.rcParams["font.family"]="sans-serif"

fig, ax = plt.subplots()
# 取消邊框
for key, spine in ax.spines.items():
    # 'left', 'right', 'bottom', 'top'
    if key == 'left' or key == 'right':
        spine.set_visible(False)
plt.xticks(np.arange(6), ('','t1', 't2', 't3', 't4', 't5'))
plt.yticks([])

# 上部分圖
x_mean=[0,2,3,4,7]
x_list=[1,2,3,4,5]

x=np.array(x_list)
y=np.array([4,9,6,8,3])
y_mean=np.mean(y).repeat(5)
#plt.plot(x,y,'ro')
plt.plot(x,y,color='red', marker='o', linestyle='dashed',linewidth=0, markersize=12)
plt.plot(x_mean,y_mean,'k--')
x_smooth = np.linspace(x.min(),x.max(),300) #300 represents number of points to make between T.min and T.max
y_smooth = make_interp_spline(x, y)(x_smooth)
plt.plot(x_smooth,y_smooth,'r--')
plt.text(-1.7,np.mean(y),r'X均值'+r'$:\mu_x$', fontdict={'size':16,'color':'r'})

# 下部分圖
y2=np.array([14,19,16,18,13])
y2_mean=np.mean(y2).repeat(5)
#plt.plot(x,y,'ro')
plt.plot(x,y2,color='red', marker='o', linestyle='dashed',linewidth=0, markersize=12)
plt.plot(x_mean,y2_mean,'k--')
x2_smooth = np.linspace(x.min(),x.max(),300) #300 represents number of points to make between T.min and T.max
y_smooth = make_interp_spline(x, y2)(x2_smooth)
plt.plot(x_smooth,y_smooth,'r--')
plt.text(-1.7,np.mean(y2),r'Y均值'+r'$:\mu_y$', fontdict={'size':16,'color':'r'})

# 畫虛線
d_y=np.linspace(0,22,10)
for i in range(len(x_list)):
    d_x=np.array([x_list[i] for j in range(10)])
    plt.plot(d_x,d_y,'g--',linewidth=0.5)

# t時刻標注
plt.show()

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM