Python做散点图
时间: 2023-12-25 15:28:06 浏览: 30
以下是使用Python绘制散点图的示例代码:
```python
import matplotlib.pyplot as plt
# 定义x和y的值
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# 绘制散点图
plt.scatter(x, y)
# 添加标题和坐标轴标签
plt.title("Scatter Plot Example")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
# 显示图形
plt.show()
```
上述代码中,我们首先导入了matplotlib.pyplot库,然后定义了x和y的值。接着,我们使用plt.scatter()函数绘制了散点图,并使用plt.title()、plt.xlabel()和plt.ylabel()函数添加了标题和坐标轴标签。最后,我们使用plt.show()函数显示了图形。
相关问题
python 做折线图
要在 Python 中做折线图,可以使用 Matplotlib 库。以下是一个简单的示例代码:
```python
import matplotlib.pyplot as plt
# x 和 y 坐标数据
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# 绘制折线图
plt.plot(x, y)
# 添加 x 和 y 轴标签
plt.xlabel('X Label')
plt.ylabel('Y Label')
# 显示图形
plt.show()
```
这个例子会绘制一条横坐标为 1 到 5,纵坐标为 2 到 10 的折线。你可以根据自己的数据来修改 x 和 y 的值。还可以通过调整线条颜色、粗细、样式等参数来自定义图形。
python折线散点图
要在Python中绘制折线散点图,你可以使用matplotlib库。以下是一个简单的示例代码:
```python
import matplotlib.pyplot as plt
# 创建数据
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# 绘制散点图
plt.scatter(x, y, color='red', label='散点图')
# 绘制折线图
plt.plot(x, y, color='blue', label='折线图')
# 添加图例和标签
plt.legend()
plt.xlabel('X轴')
plt.ylabel('Y轴')
plt.title('折线散点图示例')
# 显示图形
plt.show()
```