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])

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