pandas 两列数据直方图
时间: 2023-11-23 08:58:17 浏览: 308
以下是使用pandas绘制两列数据直方图的方法:
假设我们要绘制homes数据集中的sqft_living和price两列数据的直方图。
```python
import pandas as pd
import matplotlib.pyplot as plt
# 读取数据
homes = pd.read_excel('C:/Tools/Python/Pandas/014/home_data.xlsx')
# 绘制直方图
homes[['sqft_living', 'price']].plot.hist(alpha=0.5, bins=50)
# 设置图例和标题
plt.legend(['sqft_living', 'price'])
plt.title('Histogram of sqft_living and price')
# 显示图形
plt.show()
```
运行以上代码,即可得到sqft_living和price两列数据的直方图。
相关问题
pandas 两列数据累积分布的关系,以及作图
在 Pandas 中,可以使用 `DataFrame.plot()` 方法将两列数据的累计分布关系作图。
首先,需要载入所需的库:
```python
import pandas as pd
import matplotlib.pyplot as plt
```
然后,假设你有一个名为 `df` 的 DataFrame,包含两列数据 `col1` 和 `col2`。你可以使用如下代码绘制累计分布关系图:
```python
df[['col1', 'col2']].plot.cumulative(title='Cumulative distribution of col1 and col2')
plt.show()
```
这将在图中绘制两条曲线,分别表示 `col1` 和 `col2` 的累计分布关系。
如果你想要更多的控制,也可以使用 `DataFrame.plot.hist()` 方法绘制直方图,并使用 `cumsum()` 方法计算累计值,然后使用 `plot()` 方法绘制曲线图。
例如:
```python
df[['col1', 'col2']].plot.hist(density=True, cumulative=True, histtype='step')
plt.show()
```
这将绘制出累计分布图,其中两条曲线分别表示 `col1` 和 `col2` 的累计分布。
希望这能帮到你!
python读取csv中的数据,用概率和时间两列绘制直方图
在Python中读取CSV文件并使用概率和时间两列绘制直方图,可以使用`pandas`库来处理数据,并使用`matplotlib`库来绘制图表。以下是一个示例代码:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 读取CSV文件
data = pd.read_csv('your_file.csv')
# 假设CSV文件中有两列分别为'probability'和'time'
probability = data['probability']
time = data['time']
# 绘制概率的直方图
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.hist(probability, bins=20, color='blue', alpha=0.7)
plt.title('Probability Histogram')
plt.xlabel('Probability')
plt.ylabel('Frequency')
# 绘制时间的直方图
plt.subplot(1, 2, 2)
plt.hist(time, bins=20, color='green', alpha=0.7)
plt.title('Time Histogram')
plt.xlabel('Time')
plt.ylabel('Frequency')
plt.tight_layout()
plt.show()
```
在这个示例中,我们首先使用`pandas`库的`read_csv`函数读取CSV文件。然后,我们提取出`probability`和`time`两列的数据。最后,我们使用`matplotlib`库的`hist`函数分别绘制这两列数据的直方图。
阅读全文
相关推荐















