Matplotlib科研图表

Matplotlib科研图表

1 折线图

1-1 Matplotlib 的子库 Pyplot

plot 函数: 线条属性

1
2
3
4
plot(x1,y1,[fmt],x2,y2,[fmt],....,** kwargs)
# x,y 数据
# [fmt]: 基本格式(颜色,标记,线条)
# ** kwargs: 标签,线宽等
图片名称
  • 小例子
1
2
3
4
5
6
7
8
9
10
11
import matplotlib.pyplot as plt

x1 = [0,1,2,3,4,5,6]
y1 = [0,23,12,56,73,10,65]

x2 = [0,1,2,3,4,5,6]
y2 = [56,13,67,12,64,78,13]

plt.plot(x1,y1,'r',x2,y2,'c')

plt.savefig('fig.png', dpi=100)
  • 基本格式(颜色,标记,线条)
图片名称
1
2
3
4
5
6
7
8
9
10
11
12
13
import matplotlib.pyplot as plt

x1 = [0,1,2,3,4,5,6]
y1 = [0,23,12,56,73,10,65]

x2 = [0,1,2,3,4,5,6]
y2 = [56,13,67,12,64,78,13]

plt.plot(x1,y1,'-ro',x2,y2,'-.cs')
# -ro 实线,红色,圆点
# -.cs 点划线,青绿色,方点

plt.savefig('fig.png', dpi=100)
  • 画了个丑图
图片名称
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import matplotlib.pyplot as plt
import numpy as np

x1 = np.arange(0,6,0.1)
y1 = 60*np.sin(x1/2)

x2 = [0,1,2,3,4,5,6]
y2 = [56,13,67,12,64,78,13]

plt.plot(x1,y1,'-r',x2,y2,'-.cs',ms=20)
# -r 实线,红色
# -.cs 点划线,青绿色,方点, ms 定义大小

plt.savefig('fig.png', dpi=100)
  • ** kwargs
图片名称
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import matplotlib.pyplot as plt
import numpy as np

x1 = np.arange(0,6,0.1)
y1 = 60*np.sin(x1/2)

x2 = [0,1,2,3,4,5,6]
y2 = [56,13,67,12,64,78,13]

plt.plot(x1,y1,'-r',x2,y2,'-cs',linewidth = '10',ms = '20')
# -r 实线,红色
# -.cs 点划线,青绿色,方点, ms 定义大小
# linewidth 定义线宽

plt.savefig('fig.png', dpi=100)

xlabel & ylabel 函数:坐标轴标签属性

图片名称
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import matplotlib.pyplot as plt
import numpy as np

# 图片中的字体 宋体(STSong),仿宋(STFangsong),黑体(SimHei)
font={'size':16, # 坐标轴
'color':'black',
'family':'STFangsong'
}

x1 = np.arange(0,6,0.1)
y1 = 60*np.sin(x1/2)

plt.plot(x1,y1,'-c')

plt.xlabel("价格 $x$ 元",fontdict=font)
plt.ylabel("销售量 $n$ 件",fontdict=font)

plt.savefig('fig.png', dpi=100)

图例&标题

图片名称
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import matplotlib.pyplot as plt

font_title={'size':24, # 大标题
'y': 1.05, # 调整标题距离
'family':'STFangsong'
}

font={'size':16, # 坐标轴
'color':'black',
'family':'STFangsong'
}

x1 = [0,1,2,3,4,6,7]
y1 = [51,33,47,72,61,18,33]

x2 = [0,1,2,3,4,5,6]
y2 = [56,13,67,12,64,78,13]

# 设置图例
plt.plot(x1,y1,'-ro',label='不锈钢')
plt.plot(x2,y2,'-cs',label='铝 ')
plt.legend(prop={'family':'STFangsong','size':10})

plt.xlabel("价格 $x$ 元",fontdict=font)
plt.ylabel("销售量 $n$ 件",fontdict=font)

plt.title("商品价格与销售量的关系",fontdict = font_title)

plt.savefig('fig.png', dpi=100)

2 温度云图

import matplotlib.pyplot as plt
import matplotlib.tri as tri
from matplotlib.pyplot import figure,show,rc
import numpy as np
import pandas as pd

作者

缪红林

发布于

2021-09-04

更新于

2021-09-04

许可协议

评论