python 画线箱图
时间: 2023-08-27 11:22:46 浏览: 55
要在Python中绘制箱线图,你可以使用matplotlib库。下面是一个简单的例子来帮助你开始:
```python
import matplotlib.pyplot as plt
# 创建数据
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# 绘制箱线图
plt.boxplot(data)
# 添加标题和标签
plt.title('Box Plot')
plt.xlabel('Data')
# 显示图形
plt.show()
```
这是一个绘制简单箱线图的基本示例。你可以根据自己的数据进行适当的修改和调整。
相关问题
python turtle画药箱
Python的turtle库是一个很好用的绘图工具,可以用来画各种图形,包括药箱。下面是一个简单的药箱绘制过程:
首先,我们需要导入turtle库,然后创建一个画布和一个画笔对象:
```python
import turtle
canvas = turtle.Screen()
pen = turtle.Turtle()
```
然后,我们可以设置画笔的颜色和线条宽度:
```python
pen.color('red', 'yellow')
pen.width(3)
```
接下来,我们可以开始绘制药箱的边框,这里我们使用turtle的forward和left方法来绘制:
```python
pen.begin_fill()
pen.forward(200)
pen.left(90)
pen.forward(100)
pen.left(90)
pen.forward(200)
pen.left(90)
pen.forward(100)
pen.end_fill()
```
接着,我们绘制药箱的分隔线,同样使用forward和left方法:
```python
pen.penup()
pen.goto(0, 50)
pen.pendown()
pen.forward(200)
```
然后,我们绘制药箱的药品名称,使用turtle的write方法:
```python
pen.penup()
pen.goto(-80, 20)
pen.pendown()
pen.write('药品名称', font=('Arial', 12, 'normal'))
```
最后,我们可以画出药箱的药品数量,同样使用turtle的write方法:
```python
pen.penup()
pen.goto(-80, -10)
pen.pendown()
pen.write('药品数量', font=('Arial', 12, 'normal'))
```
完整的代码如下:
```python
import turtle
canvas = turtle.Screen()
pen = turtle.Turtle()
pen.color('red', 'yellow')
pen.width(3)
pen.begin_fill()
pen.forward(200)
pen.left(90)
pen.forward(100)
pen.left(90)
pen.forward(200)
pen.left(90)
pen.forward(100)
pen.end_fill()
pen.penup()
pen.goto(0, 50)
pen.pendown()
pen.forward(200)
pen.penup()
pen.goto(-80, 20)
pen.pendown()
pen.write('药品名称', font=('Arial', 12, 'normal'))
pen.penup()
pen.goto(-80, -10)
pen.pendown()
pen.write('药品数量', font=('Arial', 12, 'normal'))
turtle.done()
```
Python 画含多个箱线子图的代码
下面是一个使用 Python 和 Matplotlib 库绘制含有多个箱线子图的示例代码:
```python
import matplotlib.pyplot as plt
import numpy as np
# 生成数据
data1 = np.random.normal(100, 10, 200)
data2 = np.random.normal(80, 30, 200)
data3 = np.random.normal(90, 20, 200)
data4 = np.random.normal(70, 25, 200)
# 创建子图
fig, axs = plt.subplots(1, 4, figsize=(15, 5), sharey=True)
# 绘制箱线图
axs[0].boxplot(data1)
axs[0].set_title('Data 1')
axs[1].boxplot(data2)
axs[1].set_title('Data 2')
axs[2].boxplot(data3)
axs[2].set_title('Data 3')
axs[3].boxplot(data4)
axs[3].set_title('Data 4')
# 添加标题和标签
fig.suptitle('Boxplot Example')
for ax in axs:
ax.set_xticklabels([''])
plt.show()
```
这个示例代码将生成四个包含不同数据集的箱线子图,每个子图都有自己的标题。`fig.suptitle()`方法添加一个主标题,而`set_xticklabels()`方法用于删除 X 轴的标签。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)