jupter中线形图,柱形图,饼图,直方图,小提琴图如何展示
时间: 2024-04-30 13:22:02 浏览: 31
在 Jupyter 中展示不同类型的图表需要使用不同的 Python 库。以下是一些常用的库及其对应的图表类型:
- Matplotlib:线形图、柱形图、饼图、直方图、小提琴图等
- Seaborn:线形图、柱形图、饼图、直方图、小提琴图等
- Plotly:线形图、柱形图、饼图等
- Bokeh:线形图、柱形图、饼图等
以下是使用 Matplotlib 和 Seaborn 库展示不同类型图表的示例代码:
### 线形图
```python
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.show()
```
```python
import seaborn as sns
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
sns.lineplot(x, y)
```
### 柱形图
```python
import matplotlib.pyplot as plt
import numpy as np
x = ['A', 'B', 'C', 'D', 'E']
y = [10, 7, 12, 5, 8]
plt.bar(x, y)
plt.show()
```
```python
import seaborn as sns
import numpy as np
x = ['A', 'B', 'C', 'D', 'E']
y = [10, 7, 12, 5, 8]
sns.barplot(x, y)
```
### 饼图
```python
import matplotlib.pyplot as plt
labels = ['A', 'B', 'C', 'D', 'E']
sizes = [15, 30, 45, 10, 5]
plt.pie(sizes, labels=labels)
plt.show()
```
```python
import seaborn as sns
labels = ['A', 'B', 'C', 'D', 'E']
sizes = [15, 30, 45, 10, 5]
sns.pieplot(sizes, labels=labels)
```
### 直方图
```python
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(1000)
plt.hist(x, bins=30)
plt.show()
```
```python
import seaborn as sns
import numpy as np
x = np.random.randn(1000)
sns.histplot(x, bins=30)
```
### 小提琴图
```python
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(100)
y = np.random.randn(100)
plt.violinplot([x, y])
plt.show()
```
```python
import seaborn as sns
import numpy as np
x = np.random.randn(100)
y = np.random.randn(100)
sns.violinplot([x, y])
```