python绘制多条线图图
时间: 2023-07-08 11:49:02 浏览: 62
要绘制多条线图,可以使用matplotlib库。以下是一个简单的示例代码:
```python
import matplotlib.pyplot as plt
# x轴数据
x = [1, 2, 3, 4, 5]
# y轴数据
y1 = [1, 3, 2, 4, 5]
y2 = [2, 4, 1, 3, 6]
y3 = [3, 1, 4, 2, 7]
# 绘制多条线图
plt.plot(x, y1, label='line 1')
plt.plot(x, y2, label='line 2')
plt.plot(x, y3, label='line 3')
# 设置图例位置
plt.legend(loc='best')
# 设置x轴和y轴标签
plt.xlabel('x')
plt.ylabel('y')
# 设置标题
plt.title('Multiple Line Chart')
# 显示图形
plt.show()
```
在这个示例中,我们创建了3个不同的y轴数据集,然后使用`plt.plot()`函数绘制了这些数据集。我们还使用`plt.legend()`函数添加了图例,并使用`plt.xlabel()`和`plt.ylabel()`函数设置了x轴和y轴标签。最后,我们使用`plt.title()`函数设置了图形标题,并使用`plt.show()`函数显示图形。
相关问题
python绘制统计柱状图图实战
当然!下面是一个使用Python中的matplotlib库绘制统计柱状图的示例代码:
```python
import matplotlib.pyplot as plt
# 创建数据
x = ['A', 'B', 'C', 'D', 'E']
y = [20, 35, 30, 25, 40]
# 创建一个figure和一个子图
fig, ax = plt.subplots()
# 绘制柱状图
ax.bar(x, y)
# 添加标题和标签
ax.set_title('Statistical Bar Chart')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
# 显示图形
plt.show()
```
在这个示例中,我们创建了两个列表x和y,分别表示x轴和y轴的数据。然后,我们使用`ax.bar()`函数绘制了柱状图。最后,我们添加了标题和标签,并通过`plt.show()`显示图形。
你可以根据自己的需求修改和扩展这段代码。例如,你可以添加图例、调整柱状图的颜色、设置柱状图的宽度等。希望对你有所帮助!如果你有任何其他问题,请随时提问。
python画大耳朵图图
对于用Python画大耳朵图图,你可以使用Python中的绘图库来实现。比较常用的绘图库有Matplotlib和Turtle。以下是使用Matplotlib库绘制大耳朵图图的代码示例:
```python
import matplotlib.pyplot as plt
# 创建一个画布
fig, ax = plt.subplots()
# 绘制大耳朵
ear_x = [0, 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]
ear_y = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2]
ax.plot(ear_x, ear_y, color='black')
# 添加眼睛
eye1 = plt.Circle((7, 9), 1, color='black')
eye2 = plt.Circle((21, 9), 1, color='black')
ax.add_patch(eye1)
ax.add_patch(eye2)
# 设置坐标轴范围
ax.set_xlim(0, 28)
ax.set_ylim(0, 16)
# 关闭坐标轴刻度
ax.set_xticks([])
ax.set_yticks([])
# 显示图像
plt.show()
```