matplotlib-plot-style


style

1.繪制x=1
2.不同線寬

  • enumerate(Widths)
    3.線型(實線,虛線,點划線)
  • linestyle
  • set_dashes
    4.自動設置線顏色
    5.點的顯示形式
  • marker
  • markersize
  • markeredgecolor
  • markerfacecolor
    6.柱狀圖及其填充
  • axes.bar
  • axes.bar( .5+i, 1, hatch='/', color='white', edgecolor='blue',)

x=1

code

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

#Data
Y=np.linspace(0,1,12)
X=np.ones(Y.size)

#figure
fig = plt.figure(figsize=(8,6), dpi=72, facecolor='white')
axes = plt.subplot(111)

#plot 
axes.plot( (1+0)*X, Y, linewidth=0.25, color='blue')

plt.show()
 

Keypoints

axes.plot( (1+0)*X, Y, linewidth=0.25, color='blue')

X=[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

Y=[ 0. 0.09090909 0.18181818 0.27272727 0.36363636 0.45454545
0.54545455 0.63636364 0.72727273 0.81818182 0.90909091 1. ]

Result

style-01.png


不同線寬

code

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

# Data to be represented
Y = np.linspace(0,1,12)
print Y

X = np.ones(Y.size)
print X

W = [0.25,0.50,0.75,1,2,3,4,5,6,7,8]  #linewidth 
print W

# Actual plotting
fig = plt.figure(figsize=(8,6), dpi=72, facecolor='white')
axes = plt.subplot(111)

for i,w in enumerate(W):
    axes.plot( (1+i)*X, Y, linewidth=w, color='blue')

# X,Y axes lable
axes.set_xlim(0,len(W)+1)
axes.set_yticks([])
axes.set_xticks(np.arange(1,len(W)+1))
axes.set_xticklabels(['%.2f' % w for w in W])

plt.show()

Keypoints

for i,w in enumerate(W):
axes.plot( (1+i)*X, Y, linewidth=w, color='blue')


![enumerate-help.png](https://raw.githubusercontent.com/urmyfaith/urmyfaith.github.io/master/matplot/matplotGallery/images/enumerate-help.png)

繪制12條直線,X=1,...X=12

### Result

![style-02.png](https://raw.githubusercontent.com/urmyfaith/urmyfaith.github.io/master/matplot/matplotGallery/images/style-02.png)

隱藏Y軸刻度,替換X軸標簽后的圖形

![style-03.png](https://raw.githubusercontent.com/urmyfaith/urmyfaith.github.io/master/matplot/matplotGallery/images/style-03.png)

----

## 線型(實線,虛線,點划線)

### code

```python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

# Data to be represented
X = np.linspace(0,1,10)
Y = np.ones(X.size)

# Actual plotting
fig = plt.figure(figsize=(8,6), dpi=72, facecolor="white")
axes = plt.subplot(111,aspect=1)
axes.plot( X, Y*0.1, color = 'blue', linewidth=2, linestyle="-" )
axes.plot( X, Y*0.2, color = 'blue', linewidth=2, linestyle="--" )
axes.plot( X, Y*0.3, color = 'blue', linewidth=2, linestyle="-." )
axes.plot( X, Y*0.4, color = 'blue', linewidth=2, linestyle=":" )
line, = axes.plot( X, Y*0.5, color = 'blue', linewidth=2, linestyle="-" )
line.set_dashes([20,2])
line, = axes.plot( X, Y*0.6, color = 'blue', linewidth=2, linestyle="-" )
line.set_dashes([2,20])
line, = axes.plot( X, Y*0.7, color = 'blue', linewidth=2, linestyle="-" )
line.set_dashes((40,5,5,5))
line, = axes.plot( X, Y*0.8, color = 'blue', linewidth=2, linestyle="-" )
line.set_dashes((40,5,5,5,5,5))
line, = axes.plot( X, Y*0.9, color = 'blue', linewidth=2, linestyle="-" )
line.set_dashes((40,5,5,5,5,5,40,5))


axes.set_xlim(X.min(),X.max())
axes.set_ylim(0,1)
axes.set_xticks([])
axes.set_yticks(np.arange(1,10)/10.0)
axes.set_yticklabels(("-","--","-.",":",
                      "(20,2)", "(2,20)", "(40,5,5,5)",
                      "(40,5,5,5,5,5,5)", "(40,5,5,5,5,40)"))
plt.show()

Keypoints

axes.plot( X, Y*0.1, color = 'blue', linewidth=2, linestyle="-")

linestyle="-" 線型:

  • --
  • -.
  • :

控制點划線的點,空格,划的長度:

line, = axes.plot( X, Y*0.9, color = 'blue', linewidth=2, linestyle="-" )
line.set_dashes((40,5,5,5,5,5,40,5))

40划5空格5點5空格5點5空格40划5空格

help-set-dashes.png

Result

style-04.png


自動設置線顏色

code

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

# Data to be represented
Y = np.linspace(0,1,12)
X = np.ones(Y.size)

# Actual plotting
fig = plt.figure(figsize=(8,6), dpi=72, facecolor="white")
axes = plt.subplot(111)

lineNumbers=9
for i in range(lineNumbers):
    axes.plot( (1+i)*X, Y, linewidth=4)

axes.set_xlim(0,lineNumbers+1)
axes.set_yticks([])
axes.set_xticks(np.arange(1,lineNumbers+1))

plt.show()   

Keypoints

這里用循環繪制了9條直線,但是這里是自動填充顏色的?

Result

style-05.png


5. 點的顯示形式

code

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

# Data to be represented
Y = np.linspace(0,1,12)
X = np.ones(Y.size)
markers = ['.',',','o','v','^','<','>','1','2','3','4',
           's','p','*','h','H','+','x','D','d','|','_', r'$\clubsuit$']

# Actual plotting
fig = plt.figure(figsize=(8,6), dpi=72, facecolor="white")
axes = plt.subplot(111)
for i,marker in enumerate(markers):
    axes.plot( (1+i)*X, Y, color = '0.9', linewidth=1,
               markersize = 13, marker=marker,
               markeredgecolor = '0.10', markerfacecolor = '0.75')

axes.set_xlim(0,len(markers)+1)
axes.set_ylim(Y.min(),Y.max())
axes.set_yticks([])
axes.set_xticks(np.arange(1,len(markers)+1))
axes.set_xticklabels(markers)

plt.show()

Keypoints

   axes.plot( (1+i)*X, Y, color = '0.9', linewidth=1,
               markersize = 13, marker='x',
               markeredgecolor = '0.10', markerfacecolor = '0.75')

help-markers.png

標記的大小,類型,邊緣顏色,前景色

Resutl

style-markers.png


6. 柱狀圖及其填充

code

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

# Data to be represented
X = np.linspace(0,1,10)
Y = np.ones(X.size)
patterns = ('/','//','-', '+', 'x', '\\', '\\\\', '*', 'o', 'O', '.')

# Actual plotting
fig = plt.figure(figsize=(8,6), dpi=72, facecolor="white")
axes = plt.subplot(111)
for i,pattern in enumerate(patterns):
    axes.bar( .5+i, 1, hatch=pattern, color='white', edgecolor='blue',)
    
axes.set_xlim(0,len(patterns)+.5)
axes.set_ylim(0,1)

axes.set_yticks([])
axes.set_xticks(np.arange(1,len(patterns)+1))
axes.set_xticklabels(patterns)

plt.show()

Keypoints

axes.bar( .5+i, 1, hatch=pattern, color='white', edgecolor='blue',)

help-bar-01.png
help-bar-02.png

hatch,柱狀圖的填充:
help-set-hatch.png

Result

style-bar.png

style-bar-hatch.png


免責聲明!

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



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