matplotlib——基本屬性設置-字體字號顏色描邊保存高清圖片座標系

目錄

一   基本設置選項:

1,顏色選取:

2,設置中文以及notebook中設置的語句

3,在子圖中顯示圖像

4,修改已經存在的窗口大小-(不怎麼用)

5,標記

6,描邊

7,設置字體相關屬性比如字號大小,字體

設置各種地方的字體

8 保存高清圖片

9 設置窗口大小

設置圖像窗口大小

10 在座標系中新建一個座標系

應用這個原理結合海圖圖片,實現座標軸和圖片經緯度一一對應--待完善

座標系中加載圖片--和figure加載圖片不一樣--注意座標加載圖片後已經不再是我們用的座標系,y軸向下爲正了,難怪加載完圖片後,怎麼畫不出來圖片。

這是加載圖片後坐標軸變了--當下面這種情況也會發生y軸向下的情況


 

導入包:

import matplotlib.pyplot as plt

一   基本設置選項:

1,顏色選取:

可視化命名顏色(官網)好用但是不能複製其內容,這個網址是可以複製的css命名及其16進制的顏色值

  • color='#ffff00' 
  • color =(0, 0, 255)表示爲紅色
  • color = 'r'   color = 'red'
plt.Polygon([[x, y],[x-1000, y+2000],[x+1000, y-2000]], color='#ffff00', alpha=1)

2,設置中文以及notebook中設置的語句

'''顯示中文'''
plt.rcParams['font.sans-serif']=['SimHei']   #這兩行用來顯示漢字
plt.rcParams['axes.unicode_minus'] = False

'''notebook 中顯示'''
%matplotlib inline

3,在子圖中顯示圖像

fig = plt.figure(figsize=(10,10))
ax1 = fig.add_subplot(221)  #這裏的111 必須填寫,不知道什麼意思  分成1行1列的第一個圖,應該是(1,1,1)這樣寫比較好
ax1 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)

ax1.set_ylabel('萬噸')
plt.grid(True)
ax1.legend()

4,修改已經存在的窗口大小-(不怎麼用)

fig = plt.figure(figsize=(10,5))
fig1 = plt.gcf()
fig1.set_size_inches(5,2,forward=True)

5,標記

在matplotlib中繪圖標誌有兩種,一種是點標誌(marker),一種是線標誌(linestle),可選的參數如下。

       linestyle:linestyle定義是兩點之間採用什麼方式連接。

    '-'       solid line style
    '--'      dashed line style
    '-.'      dash-dot line style
    ':'       dotted line style

       marker

'.'       point marker
','       pixel marker
'o'       circle marker
'v'       triangle_down marker
'^'       triangle_up marker
'<'       triangle_left marker
'>'       triangle_right marker
'1'       tri_down marker
'2'       tri_up marker
'3'       tri_left marker
'4'       tri_right marker
's'       square marker
'p'       pentagon marker
'*'       star marker
'h'       hexagon1 marker
'H'       hexagon2 marker
'+'       plus marker
'x'       x marker
'D'       diamond marker
'd'       thin_diamond marker
'|'       vline marker
'_'       hline marker

6,描邊

相關的關鍵字參數爲:

  • edgecolor 或 ec
  • linestyle 或 ls
  • linewidth 或 lw

7,設置字體相關屬性比如字號大小,字體

import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
import numpy as np

POINTS = 100
sin_list = [0] * POINTS
fig, ax = plt.subplots()
line_sin, = ax.plot(range(POINTS), sin_list, label='Sin() output', color='cornflowerblue')
ax.legend(loc='upper center', ncol=4, prop=font_manager.FontProperties(size=20))# 這句話是關鍵,更多字體關注官網,或者百度
'''
設置字體大小
'''
font={'family':'serif',
     'style':'italic',
    'weight':'normal',
      'color':'red',
      # 'size':16
}
plt.text(11000,36000,'OS_longitude:',fontdict=font)
plt.show()

設置各種地方的字體

 

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

dataframe = pd.read_csv("port1.csv")
x = dataframe["Year"]
y = dataframe["Total port"]
y1 = dataframe["Five port group"]

# plt.figure(figsize=(15, 6.5))
labels = [ "Coastal port", "Inland port"]
fig, ax = plt.subplots()
# ax.plot(x,y1,linewidth=5,c = "r")
# ax.scatter(x,y1,marker='x',color= 'r')

font={'family':'Times New Roman',
     # 'style':'italic',  # 斜體
    'weight':'normal',
      # 'color':'red',
      'size':14
}
ax.bar(x=x, height = y, width = 0.5, label="Total port")#width = width, align='edge',label="rainfall", color="#87CEFA")
ax.bar(x=x, height = y1, width = 0.5, label="Five port group")#width = width, align='edge',label="rainfall", color="#87CEFA")
plt.xticks(x)

# 設置座標軸刻度字體屬性
labels = ax.get_xticklabels() + ax.get_yticklabels()
[label.set_fontname('Times New Roman') for label in labels]

#設置ylabel 字體屬性
plt.ylabel("TUE",font)

# 設置圖例字體屬性
ax.legend(loc='upper left',=font)

# 保存高清圖片,dpi越大,越清晰,一定要放在show前面
plt.savefig('port.png', dpi=1100, bbox_inches='tight')
plt.show()

 

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

dataframe = pd.read_csv("aver epi.csv")
x = dataframe["Year"]
y1 = dataframe["Bohai-rim port group"]
y2 = dataframe["Yangtze River Delta port group"]
y3 = dataframe["South-east coastal port group"]
y4 = dataframe["Pearl River Delta port group"]
y5 = dataframe["South-west coastal port group"]
y6 = dataframe["whole"]

fig, ax = plt.subplots(figsize=(9,5))
ax.plot(x,y1,marker = "o",linewidth=2,label='Bohai-rim port group')
ax.plot(x,y2,marker = "x",linewidth=2,label='Yangtze River Delta port group')
ax.plot(x,y3,marker = "D",linewidth=2,label='South-east coastal port group')
ax.plot(x,y4,marker = "*",linewidth=2,label='Pearl River Delta port group')
ax.plot(x,y5,marker = "h",linewidth=2,label='South-west coastal port group')
ax.plot(x,y6,marker = "1",linewidth=2,label='whole')

font={'family':'Times New Roman',
     # 'style':'italic',  # 斜體
    'weight':'normal',
      # 'color':'red',
      'size':7
}
ax.legend(loc='best',prop=font)
labels = ax.get_xticklabels() + ax.get_yticklabels()
[label.set_fontname('Times New Roman') for label in labels]
plt.savefig('epi.png', dpi=1100, bbox_inches='tight')
plt.show()

 

 

8 保存高清圖片

# 保存高清圖片,dpi越大,越清晰,一定要放在show前面
plt.savefig('port.png', dpi=1100, bbox_inches='tight')

 

 

9 設置窗口大小

設置圖像窗口大小

10 在座標系中新建一個座標系

'''
注意:這裏應該先畫最底層的圖層,在畫最上面的圖層,因爲底層的大,上層的小,底層會把上層覆蓋掉
'''
import numpy as np
import matplotlib.pyplot as plt
#新建figure
fig = plt.figure()
#定義數據
x = [1, 2, 3, 4, 5, 6, 7]
y = [1, 7, 15, 24, 30, 50, 55]

#新建區域ax1
#figure的百分比,從figure 10%的位置開始繪製, 寬高是figure的80%
left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
#獲得繪製的句柄
ax1 = fig.add_axes([left, bottom, width, height])
ax1.plot(x, y, )
# ax1.set_title(‘area1’)
#新增區域ax2,嵌套在ax1內,看一看圖中圖是什麼樣,這就是與subplot的區別
left, bottom, width, height = 0.2, 0.6, 0.25, 0.25
#獲得繪製的句柄
ax2 = fig.add_axes([left, bottom, width, height])
ax2.plot(x,y, )
# ax2.set_title(‘area2’)
plt.show()

應用這個原理結合海圖圖片,實現座標軸和圖片經緯度一一對應--待完善

座標系中加載圖片--和figure加載圖片不一樣--注意座標加載圖片後已經不再是我們用的座標系,y軸向下爲正了,難怪加載完圖片後,怎麼畫不出來圖片。

據說OpenCV:(計算機開源視覺)比PIL更強大。原因不明來源這個博客

這個博客說在深度學習圖片處理上面,PIL更好用,原因可能是轉化的numpy格式不同

'''
使用PIL處理圖像,在使用imshow加載
'''
from PIL import Image
import matplotlib.pyplot as plt
import os

# 在figure下面建立一個座標系
left, bottom, width, height = 0., 0., 0.8, 0.8
ax1 = fig.add_axes([left, bottom, width, height])

img = Image.open(os.path.join('test.jpg'))
ax1.imshow(img)
plt.show()

'''
使用OpenCV處理   pip install opencv-python
'''
import matplotlib.pyplot as plt

import cv2

left, bottom, width, height = 0., 0., 0.8, 0.8
ax1 = fig.add_axes([left, bottom, width, height])
ax1.axis('off')
img = cv2.imread('test.jpg', cv2.IMREAD_ANYCOLOR)
ax1.imshow(img)
plt.show()

'''
figure 加載背景圖片而不是座標系加載背景圖片
使用matplotlib自帶的img讀取
'''

import matplotlib.image as img
fig = plt.figure()
# figure加載背景圖片
background = img.imread('./football.png')
fig.figimage(background)
plt.show()

這是加載圖片後坐標軸變了--當下面這種情況也會發生y軸向下的情況

ax1.invert_yaxis()這個函數可以反轉y軸方向
ax1.invert_xaxis()同理,但是圖像也跟着反轉了

'''
當兩個座標軸的參數都一樣的話也會發生ax1和ax2是一個東西,ax2的y軸和ax1的y軸一樣了,也是向下爲正
解決辦法就是設置數值不一樣,稍稍大一點,或者小一點
'''
ax1 = fig.add_axes([0., 0., 1, 1])
ax2 = fig.add_axes([0., 0., 1, 1])

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章