pandas画散点图
时间: 2023-09-06 21:06:52 浏览: 112
要使用Pandas绘制散点图,你可以使用`df.plot.scatter`方法。如果你只有一个维度的数据,可以使用`df.plot.scatter`来绘制单维度的散点图。如果你有多个维度的数据,可以使用`df.plot.scatter`来绘制交叉维度的散点图。 [1]
示例1代码如下:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 准备数据
data = {'x': [1, 2, 3, 4, 5], 'y': [2, 4, 6, 8, 10]}
df = pd.DataFrame(data)
# 绘制散点图
df.plot.scatter(x='x', y='y')
# 显示图形
plt.show()
```
示例2代码如下:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 准备数据
data = {'x': [1, 2, 3, 4, 5], 'y': [2, 4, 6, 8, 10], 'c': ['red', 'blue', 'green', 'yellow', 'purple']}
df = pd.DataFrame(data)
# 绘制散点图
df.plot.scatter(x='x', y='y', c='c')
# 显示图形
plt.show()
```
如果你想绘制散点图矩阵,可以使用`pd.plotting.scatter_matrix`方法。示例代码如下:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 准备数据
data = {'x': [1, 2, 3, 4, 5], 'y': [2, 4, 6, 8, 10], 'z': [3, 6, 9, 12, 15]}
df = pd.DataFrame(data)
# 绘制散点图矩阵
pd.plotting.scatter_matrix(df)
# 显示图形
plt.show()
```
这些示例代码可以帮助你使用Pandas绘制不同类型的散点图。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [Python Pandas 图形绘制(三):散点图(单维度和交叉维度)](https://blog.csdn.net/qq_42067550/article/details/106414534)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* [Python基于pandas绘制散点图矩阵代码实例](https://download.csdn.net/download/weixin_38742571/13713419)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文