Python matplotlib自定義刻度樣式

2021-10-09 04:54:10 字數 2367 閱讀 8916

刻度由刻度標籤和刻度線組成,如果需要進一步設定刻度樣式,就需要用到定位器刻度格式器

刻度定位器用來設定刻度線的位置,刻度格式器用來設定刻度標籤的顯示樣式。

import numpy as np

import matplotlib.pyplot as plt

from matplotlib.ticker import autominorlocator, multiplelocator, funcformatter

import matplotlib as mpl

mpl.rcparams[

'font.sans-serif']=

['simhei'

]mpl.rcparams[

'font.serif']=

['simhei'

]mpl.rcparams[

'axes.unicode_minus']=

false

# 解決儲存影象是負號'-'顯示為方塊的問題,或者轉換負號為字串

x = np.linspace(

0.5,

3.5,

100)

y = np.sin(x)

# 建立畫布

fig = plt.figure(figsize=(8

,8))

# 新增子圖,這裡 111 表示就一張圖,返回該子圖的座標系物件

axes = fig.add_subplot(

111)

# 設定座標軸的主刻度線

axes.xaxis.set_major_locator(multiplelocator(

2.0)

)# 2的倍數顯示主刻度線

axes.yaxis.set_major_locator(multiplelocator(

1.0)

)# 1的倍數顯示主刻度線

# 設定座標軸主刻度線之間的輔助刻度線

axes.xaxis.set_minor_locator(autominorlocator(4)

)# 每個主刻度線間等分8段

axes.yaxis.set_minor_locator(autominorlocator(4)

)# 每個主刻度線間等分4段

# 定義修改刻度標籤格式的函式,這裡有要求引數必須是x和pos

defminor

(x, pos)

:return f""

defmajor

(x, pos)

:return f""

# 將之前定義的函式傳入格式

axes.xaxis.set_major_formatter(funcformatter(major)

)axes.yaxis.set_major_formatter(funcformatter(major)

)axes.xaxis.set_minor_formatter(funcformatter(minor)

)axes.yaxis.set_minor_formatter(funcformatter(minor)

)# 設定刻度線和刻度標籤的樣式

axes.tick_params(axis=

"x", which=

"major"

, length=

15, width=

2.0, colors=

"r")

axes.tick_params(axis=

"y", which=

"minor"

, length=

5, width=

1.0, labelsize=

10, labelcolor=

"0.25"

)# 設定要顯示的座標範圍

axes.set_xlim(0,

4)axes.set_ylim(0,

2)# 用該子圖的座標系物件例項畫圖

axes.plot(x, y, c=

(0.25

,0.25

,1.00

), lw=

2, zorder=10)

# axes.plot(x, y, c=(0.25, 0.25, 1.00), lw=2, zorder=0)

# 設定網格線

axes.grid(linestyle=

"-", linewidth=

0.5, color=

"r", zorder=0)

# 顯示畫布

plt.show(

)

最終結果如圖:

自定義刻度 Android自定義滑動刻度進度條

做到以上的效果圖,就可以知道,兩個模式,首先,定義相應的自定義屬性,額,沒辦法,上頭要求,所以規範點 新建乙個bar attrs.xml檔案 attr declare styleable resources 然後就是 中取得相應的屬性值和設定預設值 然後修改seekbar的線的顏色和滑動塊的屬性,通...

自定義 View 迴圈滾動刻度控制項

先看效果圖 enter description here loopscaleview 是乙個自定義的刻度尺風格的選值控制項,從上面的 大家可以看到 loopscaleview 的執行效果.可以設定螢幕內顯示的刻度數,也可以設定每乙個刻度代表的值得大小。onvaluechangelistener 刻度...

android 自定義actionbar樣式和布局

1.首先,新增自定義的actionbar樣式 在style.xml中新增樣式actionbarstyle 2.在style.xml中新增activity的樣式customtheme,並在改樣式中設定actionbar的樣式為actionbarstyle 3.在manifest中為對應的activit...