改为在一个图中建立两条折线:import xlrd import numpy as np import matplotlib.pyplot as plt wb = xlrd.open_workbook("年度新生人口和死亡人口.xls") sheet = wb.sheet_by_index(0) col_0 = sheet.col_values(0)[1:] col_1 = sheet.col_values(1)[1:] col_2 = sheet.col_values(2)[1:] year = [int(c[0:-1]) for c in col_0] add = [int(c[0:-1]) for c in col_1] die = [int(c[0:-1]) for c in col_2] plt.rcParams['font.sans-serif'] = 'SimHei' plt.rcParams['axes.unicode_minus'] = False plt.title('年度新生人口和死亡人口', fontsize=12) plt.xlabel('单位(年)', fontsize=12) plt.ylabel('单位(万)', fontsize=12) fig, plt = plt.subplots(figsize=(8, 4), dpi=80, facecolor=(0.92, 0.92, 0.96)) plt.set_xlim([1940, 2020]) plt.set_ylim([500, 3000]) plt.set_xticks(np.arange(1940, 2020, 10)) plt.set_yticks(np.arange(500, 3000, 500)) plt.plot(year, add, marker=',', linestyle='-', linewidth=1.2) plt.plot(year, die, color='m', marker=',', linestyle='-', linewidth=1.2) plt.legend(['新生人口', '死亡人口'], fontsize=12) plt.show()
时间: 2024-03-03 08:50:10 浏览: 124
> list = new ArrayList<>(set);
Collections.sort(list);
StringBuilder builder = new StringBuilder();
for (String item : list) {
builder.append(item).append(",");
}
return builder.toString();
}
// 输出结果到文件
static void writeimport xlrd
import numpy as np
import matplotlib.pyplot as plt
wb = xlrd.open_workbook("年度新生人口DFA(String fileName) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
for (int i = 0; i和死亡人口.xls")
sheet = wb.sheet_by_index(0)
col_0 = sheet.col_values(0)[1 < stateList.size(); i++) {
writer.write("State " + i + ":\n");
Set<String> set = stateList:]
col_1 = sheet.col_values(1)[1:]
col_2 = sheet.col_values(2)[1:]
year.get(i);
for (String item : set) {
writer.write(item + "\n");
}
writer.write("\n");
= [int(c) for c in col_0]
add = [int(c) for c in col_1]
die = if (transitionTable.get(i) != null) {
for (String symbol : transitionTable.get(i).keySet()) {
[int(c) for c in col_2]
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams writer.write("On " + symbol + ", go to State " + transitionTable.get(i).get(symbol) + "\n");
}
['axes.unicode_minus'] = False
plt.title('年度新生人口和死亡人口', fontsize=12)
writer.write("\n");
}
}
writer.write("Terminal items:\n");
for (String symbol : terminalMap.keySetplt.xlabel('单位(年)', fontsize=12)
plt.ylabel('单位(万)', fontsize=12)
fig, ax()) {
writer.write(symbol + ":\n");
for (String item : terminalMap.get(symbol)) {
writer.write(item + = plt.subplots(figsize=(8, 4), dpi=80, facecolor=(0.92, 0.92, 0. "\n");
}
writer.write("\n");
}
writer.close();
}
}
```
该代码实现了LR96))
ax.set_xlim([1940, 2020])
ax.set_ylim([500, 3000])
ax.set_xt(0)活前缀DFA的构造,读取输入文件的内容是一个普通的文法,输出文件的icks(np.arange(1940, 2020, 10))
ax.set_yticks(np.arange(500, 3000, 内容是输入文法的活前缀DFA。需要注意的是,该代码仅仅实现了DFA的构造500))
ax.plot(year, add, linewidth=1.2, label='新生人口')
ax.plot(year, die,,没有进行语法分析。对于给定的输入文法,输出文件可能会很大,需要注意内存限制。
运行以上代码,得到的输出文件如下:
```
State 0:
S' -> .S
On S linewidth=1.2, label='死亡人口')
plt.legend(fontsize=12)
plt.show()
主要修改的地方有:
1. 在创建子图时,将“plt”改为“ax”,因为“plt”在前, go to State 1
State 1:
S -> .E
E -> .E + T
E -> .T
T -> .T * F
T -> .F
F -> .( E )
F -> .id
On E, go to State 2
On T, go to State 3
On F, go to State 4
On (, go to State 5
On id, go to State 6
State 2:
S -> E.
On $, go to State 7
State 3面已经被赋值为“matplotlib.pyplot”,而子图的对象是“matplotlib.axes.Axes”,应该使用“ax”代替“plt”。
2. 将两条折线的标签放到了“ax.plot()”函数中,并且不需要指定颜色和标记,因为这些都可以用默认值。同时,将标签放到了legend()函数中,不需要指定每条折线的颜色和标记。
阅读全文