x=[1,2,3,4,5,6,7,8,9,10,11,12,13,14] y=[27,26.5,28.5,29,30,32,32.5,30,22,22.5,25.5,29,34] z=[35,38,36,34,34,35,36,35,34,36,33,35,34,35] 使用python语言,将以上数据通过excel绘制成柱状图,条形图,折线图,面积图,散点图,饼图,环形图,气泡图,组合图,雷达图,玫瑰图,漏洞图,仪表图
时间: 2023-09-18 12:10:24 浏览: 120
11-802.1x命令
好的,以下是使用 Python 绘制各种图表的代码:
首先需要安装 matplotlib 和 pandas 库:
```python
!pip install matplotlib pandas
```
然后导入相关的库和数据:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 数据
x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
y = [27,26.5,28.5,29,30,32,32.5,30,22,22.5,25.5,29,34]
z = [35,38,36,34,34,35,36,35,34,36,33,35,34,35]
# 转换为 DataFrame
df = pd.DataFrame({'x': x, 'y': y, 'z': z})
```
接下来进行各种图表的绘制:
1. 柱状图
```python
# 柱状图
df.plot(kind='bar', x='x', y=['y', 'z'])
plt.show()
```
2. 条形图
```python
# 条形图
df.plot(kind='barh', x='x', y=['y', 'z'])
plt.show()
```
3. 折线图
```python
# 折线图
df.plot(kind='line', x='x', y=['y', 'z'])
plt.show()
```
4. 面积图
```python
# 面积图
df.plot(kind='area', x='x', y=['y', 'z'])
plt.show()
```
5. 散点图
```python
# 散点图
df.plot(kind='scatter', x='x', y='y')
plt.show()
```
6. 饼图
```python
# 饼图
df.sum().plot(kind='pie')
plt.show()
```
7. 环形图
```python
# 环形图
df.sum().plot(kind='pie', radius=1, pctdistance=0.8, autopct='%1.1f%%', wedgeprops=dict(width=0.4))
plt.show()
```
8. 气泡图
```python
# 气泡图
df.plot(kind='scatter', x='x', y='y', s=df['z']*10)
plt.show()
```
9. 组合图
```python
# 组合图
fig, axs = plt.subplots(2, 2, figsize=(10, 10))
# 柱状图
df.plot(kind='bar', x='x', y=['y', 'z'], ax=axs[0,0])
# 折线图
df.plot(kind='line', x='x', y=['y', 'z'], ax=axs[0,1])
# 面积图
df.plot(kind='area', x='x', y=['y', 'z'], ax=axs[1,0])
# 散点图
df.plot(kind='scatter', x='x', y='y', ax=axs[1,1])
plt.show()
```
10. 雷达图
```python
# 雷达图
from math import pi
# 数据
categories=['y', 'z']
N = len(categories)
values=df.loc[0,categories].values.flatten().tolist()
values += values[:1]
# 角度
angles = [n / float(N) * 2 * pi for n in range(N)]
angles += angles[:1]
# 绘图
ax = plt.subplot(111, polar=True)
# 添加标签
plt.xticks(angles[:-1], categories, color='grey', size=8)
# 添加数据
ax.plot(angles, values, linewidth=1, linestyle='solid')
# 填充颜色
ax.fill(angles, values, 'b', alpha=0.1)
plt.show()
```
11. 玫瑰图
```python
# 玫瑰图
plt.hist(df['y'], bins=6, density=False, alpha=0.5)
plt.hist(df['z'], bins=6, density=False, alpha=0.5)
plt.show()
```
12. 漏斗图
```python
# 漏斗图
from pyecharts import options as opts
from pyecharts.charts import Funnel
data = [("y", 27), ("z", 35)]
funnel = (
Funnel()
.add(
series_name="",
data_pair=data,
gap=2,
tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a} <br/>{b} : {c}%"),
label_opts=opts.LabelOpts(is_show=True),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="Funnel-基本示例"),
legend_opts=opts.LegendOpts(is_show=False),
)
.render("funnel_base.html")
)
```
13. 仪表图
```python
# 仪表图
from pyecharts import options as opts
from pyecharts.charts import Gauge, Page
gauge = (
Gauge()
.add("", [("y", 27), ("z", 35)])
.set_global_opts(title_opts=opts.TitleOpts(title="Gauge-基本示例"))
)
page = Page(layout=Page.SimplePageLayout)
page.add(gauge)
page.render("gauge_base.html")
```
以上是使用 Python 绘制各种图表的代码,其中某些图表需要使用第三方库,比如 pyecharts。如果您没有安装这些库,可以使用 pip 进行安装。
阅读全文