Matplotlib绘制经济学图表

最近写paper需要自己绘制一些图,但是在网上看了一圈都没有相关的教程,只能自己动手了。

其实是有找到一个latex的教程,但是不想学latex,于是就用python来整。代码里头都有注释,而且代码本身简单易懂,我就不多作介绍了,如果能帮上忙就再好不过。

General Equilibrium

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import matplotlib.pyplot as plt

plt.title('General Equilibrium')

# Set Range
plt.xlim(0,100)
plt.ylim(0,100)

# Demand & Supply Curve
plt.plot([10,90],[10,90], 'r')
plt.plot([10,90],[90,10], 'b')

# dashed line
plt.plot([0, 50], [50, 50], 'k--')
plt.plot([50, 50], [0, 50], 'k--')

# Dot
plt.plot(50,50,'ko')

# Text
plt.text(-8,48,'$P_E$',size=16, color='k')
plt.text(48,-8,'$Q_E$',size=16, color='k')
plt.text(-2,102,'$P$',size=16, color='k')
plt.text(102,-2,'$Q$',size=16, color='k')
plt.text(92,88,'$S$',size=16, color='k')
plt.text(92,8,'$D$',size=16, color='k')

# Get Axis Object
ax = plt.gca()

# Hide X and Y axes label marks
ax.xaxis.set_tick_params(labelbottom=False)
ax.yaxis.set_tick_params(labelleft=False)

# Hide frame
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

# Hide X and Y axes tick marks
ax.set_xticks([])
ax.set_yticks([])

plt.show()

Oligopoly Demand

Note: Kinked Demand Curve

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import matplotlib.pyplot as plt
import pandas as pd

plt.title('Oligopoly Demand Curve')

# Set Range
plt.xlim(0,100)
plt.ylim(0,100)

# Demand & Supply Curve
plt.plot([10,70],[90,70], 'r')
plt.plot([70,90],[70,10], 'b')

# dashed line
plt.plot([0, 70], [70, 70], 'k--')
plt.plot([70, 70], [0, 70], 'k--')

# Dot
plt.plot(70,70,'ko')

# Text
plt.text(-8,68,'$P_1$',size=16, color='k')
plt.text(68,-8,'$Q_1$',size=16, color='k')
plt.text(-2,102,'$P$',size=16, color='k')
plt.text(102,-2,'$Q$',size=16, color='k')
plt.text(92,8,'$D$',size=16, color='k')

# Get Axis Object
ax = plt.gca()

# Hide X and Y axes label marks
ax.xaxis.set_tick_params(labelbottom=False)
ax.yaxis.set_tick_params(labelleft=False)

# Hide frame
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

# Hide X and Y axes tick marks
ax.set_xticks([])
ax.set_yticks([])

plt.show()

Demand Curve Shifts

Example 1

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import matplotlib.pyplot as plt

plt.title('Supply & Demand Shifts')

# Set Range
plt.xlim(0,100)
plt.ylim(0,100)

# Demand & Supply Curve
plt.plot([10,90],[10,90], 'r')
plt.plot([15,55],[90,10], 'b')
plt.plot([45,85],[90,10], 'b')

# dashed line
plt.plot([0, 40], [40, 40], 'k--')
plt.plot([40, 40], [0, 40], 'k--')
plt.plot([0, 60], [60, 60], 'k--')
plt.plot([60, 60], [0, 60], 'k--')

# Dot
plt.plot(40,40,'ko')
plt.plot(60,60,'ko')

# Arrow
plt.arrow(23, 80, 20, 0, head_width=3, head_length=3, fc='k', ec='k')
plt.arrow(-11, 38, 0, 20, head_width=3, head_length=3, fc='k', ec='k', clip_on = False)
plt.arrow(39, -11, 20, 0, head_width=3, head_length=3, fc='k', ec='k', clip_on = False)

# Text
plt.text(-8,38,'$P_1$',size=16, color='k')
plt.text(38,-8,'$Q_1$',size=16, color='k')
plt.text(-8,58,'$P_2$',size=16, color='k')
plt.text(58,-8,'$Q_2$',size=16, color='k')
plt.text(-2,102,'$P$',size=16, color='k')
plt.text(102,-2,'$Q$',size=16, color='k')
plt.text(92,88,'$S$',size=16, color='k')
plt.text(54,3,'$D_1$',size=16, color='k')
plt.text(84,3,'$D_2$',size=16, color='k')

# Get Axis Object
ax = plt.gca()

# Hide X and Y axes label marks
ax.xaxis.set_tick_params(labelbottom=False)
ax.yaxis.set_tick_params(labelleft=False)

# Hide frame
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

# Hide X and Y axes tick marks
ax.set_xticks([])
ax.set_yticks([])

plt.show()

Example 2

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import matplotlib.pyplot as plt

plt.title('Supply & Demand Shifts')

# Set Range
plt.xlim(0,100)
plt.ylim(0,100)

# dashed line
#plt.plot([0, 65], [80, 80], 'k--')
plt.plot([0, 65], [50, 50], 'k--')
plt.plot([65, 65], [0, 50], 'k--')
plt.plot([35, 35], [0, 50], 'k--')

# Demand & Supply Curve
plt.plot([15,55],[10,90], 'r')
plt.plot([15,55],[90,10], 'b')
plt.plot([45,85],[90,10], 'b')
plt.plot([45,85],[10,90], 'r')

# Dot
plt.plot(35,50,'ko')
plt.plot(65,50,'ko')

# Arrow

plt.arrow(21, 85, 20, 0, head_width=3, head_length=3, fc='k', ec='k')
plt.arrow(21, 15, 20, 0, head_width=3, head_length=3, fc='k', ec='k')

# Text
plt.text(-8,48,'$P_1$',size=16, color='k')
#plt.text(-8,78,'$P_2$',size=16, color='k')
plt.text(33,-8,'$Q_1$',size=16, color='k')
plt.text(63,-8,'$Q_2$',size=16, color='k')
plt.text(-2,102,'$P$',size=16, color='k')
plt.text(102,-2,'$Q$',size=16, color='k')
plt.text(57,92,'$S_1$',size=16, color='k')
plt.text(87,92,'$S_2$',size=16, color='k')
plt.text(87,8,'$D_2$',size=16, color='k')
plt.text(57,8,'$D_1$',size=16, color='k')

# Get Axis Object
ax = plt.gca()

# Hide X and Y axes label marks
ax.xaxis.set_tick_params(labelbottom=False)
ax.yaxis.set_tick_params(labelleft=False)

# Hide frame
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

# Hide X and Y axes tick marks
ax.set_xticks([])
ax.set_yticks([])

plt.show()

Cost Curves

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import make_interp_spline

plt.title('Cost Curves (Short Run)')

# Set Range
plt.xlim(0,100)
plt.ylim(0,100)

# MC
x = np.linspace(10,80,100)
y = (0.15*x-5)**2+30
plt.plot(x, y, 'k')

# AFC
x = np.linspace(10,50,100)
y = 0.03*(x-50)**2+20
plt.plot(x, y, 'r')

# AVC
x = np.linspace(10,70,100)
y = 0.02*(x-40)**2+35
plt.plot(x, y, 'g')

# ATC
x = np.linspace(20,70,100)
y = 0.03*(x-50)**2+45
plt.plot(x, y, 'b')

# Get Axis Object
ax = plt.gca()

# Hide X and Y axes label marks
ax.xaxis.set_tick_params(labelbottom=False)
ax.yaxis.set_tick_params(labelleft=False)

# Hide frame
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

# Hide X and Y axes tick marks
ax.set_xticks([])
ax.set_yticks([])

plt.text(-8,102,'Unit Cost',size=12, color='k')
plt.text(102,-2,'QTY',size=12, color='k')
plt.text(82,80,'MC',size=12, color='k')
plt.text(72,56,'ATC',size=12, color='k')
plt.text(72,50,'AVC',size=12, color='k')
plt.text(52,19,'AFC',size=12, color='k')

plt.show()

AS-AD Model

Adverse Demand Shock

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import matplotlib.pyplot as plt

plt.title('Adverse Demand Shock')

# Set Range
plt.xlim(0,100)
plt.ylim(0,100)

# dashed line
plt.plot([0, 50], [50, 50], 'k--')
plt.plot([0, 50], [20, 20], 'k--')
plt.plot([50, 50], [0, 50], 'k--')
plt.plot([35, 35], [0, 50], 'k--')

# Demand & Supply Curve
plt.plot([50,50],[10,90], 'r')
plt.plot([10,90],[50,50], 'r')
plt.plot([15,55],[90,10], 'b')
plt.plot([30,70],[90,10], 'b')

# Dot
plt.plot(50,20,'ko')
plt.plot(50,50,'ko')
plt.plot(35,50,'ko')

# Arrow
plt.arrow(29, 85, -5, 0, head_width=3, head_length=3, fc='k', ec='k')

# Text
plt.text(-8,18,'$P_2$',size=16, color='k')
plt.text(-8,48,'$P_1$',size=16, color='k')
plt.text(48,-8,r'$\bar Y$',size=16, color='k')
plt.text(33,-8,'$Y_2$',size=16, color='k')
plt.text(-2,102,'$P$',size=16, color='k')
plt.text(102,-2,'$Y$',size=16, color='k')
plt.text(52,88,'$LRAS$',size=16, color='k')
plt.text(92,48,'$SRAS$',size=16, color='k')
plt.text(72,8,'$AD_1$',size=16, color='k')
plt.text(57,8,'$AD_2$',size=16, color='k')
plt.text(52,52,'$A$',size=16, color='k')
plt.text(37,52,'$B$',size=16, color='k')
plt.text(52,22,'$C$',size=16, color='k')

# Get Axis Object
ax = plt.gca()

# Hide X and Y axes label marks
ax.xaxis.set_tick_params(labelbottom=False)
ax.yaxis.set_tick_params(labelleft=False)

# Hide frame
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

# Hide X and Y axes tick marks
ax.set_xticks([])
ax.set_yticks([])

plt.show()

Adverse Supply Shock

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import matplotlib.pyplot as plt

plt.title('Adverse Supply Shock')

# Set Range
plt.xlim(0,100)
plt.ylim(0,100)

# dashed line
plt.plot([0, 50], [80, 80], 'k--')
plt.plot([0, 50], [50, 50], 'k--')
plt.plot([50, 50], [0, 50], 'k--')
plt.plot([35, 35], [0, 80], 'k--')

# Demand & Supply Curve
plt.plot([50,50],[10,90], 'r')
plt.plot([10,90],[50,50], 'r')
plt.plot([10,90],[80,80], 'r')
plt.plot([30,70],[90,10], 'b')

# Dot
plt.plot(50,50,'ko')
plt.plot(35,80,'ko')

# Arrow

plt.arrow(80, 53, 0, 20, head_width=3, head_length=3, fc='k', ec='k')
plt.arrow(85, 76, 0, -20, head_width=3, head_length=3, fc='k', ec='k')

# Text
plt.text(-8,78,'$P_2$',size=16, color='k')
plt.text(-8,48,'$P_1$',size=16, color='k')
plt.text(48,-8,r'$\bar Y$',size=16, color='k')
plt.text(33,-8,'$Y_2$',size=16, color='k')
plt.text(-2,102,'$P$',size=16, color='k')
plt.text(102,-2,'$Y$',size=16, color='k')
plt.text(52,88,'$LRAS$',size=16, color='k')
plt.text(92,48,'$SRAS_1$',size=16, color='k')
plt.text(92,78,'$SRAS_2$',size=16, color='k')
plt.text(72,8,'$AD_1$',size=16, color='k')
plt.text(52,52,'$A=C$',size=16, color='k')
plt.text(37,82,'$B$',size=16, color='k')

# Get Axis Object
ax = plt.gca()

# Hide X and Y axes label marks
ax.xaxis.set_tick_params(labelbottom=False)
ax.yaxis.set_tick_params(labelleft=False)

# Hide frame
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

# Hide X and Y axes tick marks
ax.set_xticks([])
ax.set_yticks([])

plt.show()

Tax

Demand Shifts

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import matplotlib.pyplot as plt

plt.title('Entry Tax on Tourists')

# Set Range
plt.xlim(0,100)
plt.ylim(0,100)

# dashed line
plt.plot([0, 40], [40, 40], 'k--')
plt.plot([0, 30], [30, 30], 'k--')
plt.plot([40, 40], [0, 40], 'k--')
plt.plot([30, 30], [0, 30], 'k--')

# Demand & Supply Curve
plt.plot([0,80],[0,80], 'r')
plt.plot([0,80],[80,0], 'b')
plt.plot([0,60],[60,0], 'b')

# Dot
plt.plot(40,40,'ko')
plt.plot(30,30,'ko')

# Arrow
plt.arrow(61, 15, -10, 0, head_width=3, head_length=3, fc='k', ec='k')

# Text
plt.text(-8,38,'$P_1$',size=16, color='k')
plt.text(-8,28,'$P_2$',size=16, color='k')
plt.text(28,-8,'$Q_2$',size=16, color='k')
plt.text(38,-8,'$Q_1$',size=16, color='k')
plt.text(-2,102,'$P$',size=16, color='k')
plt.text(102,-2,'$Q$',size=16, color='k')
plt.text(82,82,'$S$',size=16, color='k')
plt.text(82,2,'$D_1$',size=16, color='k')
plt.text(62,2,'$D_2$',size=16, color='k')

# Get Axis Object
ax = plt.gca()

# Hide X and Y axes label marks
ax.xaxis.set_tick_params(labelbottom=False)
ax.yaxis.set_tick_params(labelleft=False)

# Hide frame
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

# Hide X and Y axes tick marks
ax.set_xticks([])
ax.set_yticks([])

plt.show()

Tax Effects

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import matplotlib.pyplot as plt
from matplotlib.patches import Patch

plt.title('Entry Tax on Tourists')

# Set Range
plt.xlim(0,100)
plt.ylim(0,100)

# dashed line
plt.plot([0, 30], [30, 30], 'k--')
plt.plot([30, 30], [0, 50], 'k--')
plt.plot([0, 30], [50, 50], 'k--')

# Demand & Supply Curve
plt.plot([0,80],[0,80], 'r')
plt.plot([0,80],[80,0], 'b')
plt.plot([0,60],[60,0], 'b')

# Dot
plt.plot(30,30,'ko')

# Arrow
plt.arrow(61, 15, -10, 0, head_width=3, head_length=3, fc='k', ec='k')

# Text
plt.text(-8,28,'$P_E$',size=16, color='k')
plt.text(28,-8,'$Q_E$',size=16, color='k')
plt.text(-2,102,'$P$',size=16, color='k')
plt.text(102,-2,'$Q$',size=16, color='k')
plt.text(82,82,'$S$',size=16, color='k')
plt.text(82,2,'$D_1$',size=16, color='k')
plt.text(62,2,'$D_2$',size=16, color='k')

# Get Axis Object
ax = plt.gca()

# Hide X and Y axes label marks
ax.xaxis.set_tick_params(labelbottom=False)
ax.yaxis.set_tick_params(labelleft=False)

# Hide frame
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

# Hide X and Y axes tick marks
ax.set_xticks([])
ax.set_yticks([])

ax.fill_between([0, 30], [0, 30], 30, color='r', alpha=0.2)
ax.fill_between([0, 30], [50, 50], 30, color='g', alpha=0.2)
ax.fill_between([0, 30], [80, 50], 50, color='b', alpha=0.2)
ax.fill_between([30, 40], [50, 40], [30, 40], color='y', alpha=0.3)

legend_elements = [
Patch(facecolor='r', edgecolor='r', alpha =0.5, label='Producer Surplus'),
Patch(facecolor='g', edgecolor='g', alpha =0.5, label='Tax Revenue'),
Patch(facecolor='b', edgecolor='b', alpha =0.5, label='Consumer Surplus'),
Patch(facecolor='y', edgecolor='y', alpha =0.5, label='Deadweight Loss'),
]

ax.legend(handles=legend_elements, loc='best')

plt.show()

Frontier

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
30
31
32
33
34
35
36
import matplotlib.pyplot as plt
import numpy as np

plt.title('Frontier')

# Set Range
plt.xlim(0,100)
plt.ylim(0,100)

# Red Frontier
x = np.linspace(0, 80, 100)
y = (6400-x**2)**0.5
plt.plot(x, y, 'k', linewidth=2)

# Blue Frontier
x2 = np.linspace(100, 20, 100)
y2 = 100-((6400-x**2)**0.5)
plt.plot(x2, y2, 'k', linewidth=2)

# Get Axis Object
ax = plt.gca()

# Fill Color
# https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.fill_between.html
ax.fill_between(x, y, color='none', hatch='////', edgecolor='r')
ax.fill_between(x2, y2, max(y2), color='b', alpha=0.1)

# Hide X and Y axes label marks
ax.xaxis.set_tick_params(labelbottom=False)
ax.yaxis.set_tick_params(labelleft=False)

# Hide X and Y axes tick marks
ax.set_xticks([])
ax.set_yticks([])

plt.show()