1. 使用Path绘制如下图所示三角形。
时间: 2024-05-06 22:19:59 浏览: 122
```
import matplotlib.pyplot as plt
# 创建Path对象
path = plt.Path([(0, 0), (0.5, 1), (1, 0)])
# 创建一个图形对象
fig, ax = plt.subplots()
# 绘制路径
patch = plt.PathPatch(path, facecolor='none', edgecolor='black')
ax.add_patch(patch)
# 设置坐标轴范围
ax.set_xlim(-0.1, 1.1)
ax.set_ylim(-0.1, 1.1)
# 显示结果
plt.show()
```
![image.png](attachment:image.png)
相关问题
使用Path绘制如下图所示三角形
```
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Triangle extends JPanel {
private static final long serialVersionUID = 1L;
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Path2D path = new Path2D.Float();
path.moveTo(50, 50);
path.lineTo(150, 50);
path.lineTo(100, 150);
path.closePath();
g2d.draw(path);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Triangle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Triangle());
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
运行结果如下图所示:
![image](https://user-images.githubusercontent.com/74979660/136699555-5ec9b5a6-a61c-4c8f-8c6a-7cd19b0c9b7b.png)
阅读全文