#绘制饼图可视化红酒品质"quality"的分布情况,要求: #标签为红酒的品质等级值,内标签保留两位小数,,图片保存为Figure1.png
时间: 2024-03-05 09:47:26 浏览: 65
好的,这里提供一个基于Python的解决方案。首先需要导入相关的库:matplotlib和pandas。
```
import pandas as pd
import matplotlib.pyplot as plt
```
然后我们读取数据集,假设数据集文件名为"winequality-red.csv",并且品质列名为"quality"。
```
data = pd.read_csv("winequality-red.csv")
quality = data["quality"]
```
接下来,我们可以使用pandas的value_counts()函数获取品质等级的计数。
```
counts = quality.value_counts()
```
然后,我们可以使用matplotlib的pie()函数绘制饼图。
```
plt.pie(counts, labels=counts.index.map(lambda x: "{:.2f}".format(x)), autopct="%1.1f%%")
plt.title("Red Wine Quality Distribution")
plt.savefig("Figure1.png")
plt.show()
```
完整代码如下:
```
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv("winequality-red.csv")
quality = data["quality"]
counts = quality.value_counts()
plt.pie(counts, labels=counts.index.map(lambda x: "{:.2f}".format(x)), autopct="%1.1f%%")
plt.title("Red Wine Quality Distribution")
plt.savefig("Figure1.png")
plt.show()
```
运行该代码,将会在当前目录下生成一个名为"Figure1.png"的饼图。
阅读全文
相关推荐
















