【无标题】

# 需求二
import numpy as np
import matplotlib.pyplot as plt
from math import pi

#中文
plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False

# 设置数据

categories = ['化学药', '中成药', '生物制品', '中药颗粒', '中药饮片', '保健品']
values_ynby = [33, 45, 10, 9, 4, 4]  # 云南白药的数据

values_trt = [16, 14, 6, 8, 30, 33]  # 同仁堂的数据


# 计算雷达图的角度

N = len(categories)
angles = [n / float(N) * 2 * pi for n in range(N)]
values_ynby += values_ynby[:1]
values_trt += values_trt[:1]
angles += angles[:1]

# 绘制雷达图

ax = plt.subplot(111, polar=True)
plt.xticks(angles[:-1], categories)

# 绘制第一家公司的数据

ax.plot(angles, values_ynby, 'o-', linewidth=2, label='云南白药')
ax.fill(angles, values_ynby, alpha=0.25)

# 绘制第二家公司的数据

ax.plot(angles, values_trt, 'o-', linewidth=2, label='同仁堂')
ax.fill(angles, values_trt, alpha=0.25)

# 添加标题

plt.title("两家公司各种品类药物销售额", size=20)

# 计算标签位置

label_position_ynby = np.mean(values_ynby)
label_position_trt = np.mean(values_trt)

# 使用annotate添加箭头指向阴影区域

ax.annotate('云南白药',
            xy=(angles[1], label_position_ynby), xycoords='data',
            xytext=(-90, -30), textcoords='offset points',
            arrowprops=dict(facecolor='black', shrink=0.05),
            horizontalalignment='right', verticalalignment='bottom')

ax.annotate('同仁堂',
            xy=(angles[4], label_position_trt), xycoords='data',
            xytext=(80, 30), textcoords='offset points',
            arrowprops=dict(facecolor='black', shrink=0.05),
            horizontalalignment='left', verticalalignment='bottom')

# 显示图形

plt.show()

KK

 需求三
import matplotlib.pyplot as plt
import numpy as np

#中文
plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False

# 提供的数据

years = ['2018', '2019', '2020', '2021']
revenue_keli = np.array([7150, 9227, 10016, 13400])
growth_keli = np.array([10.01, 29.05, 8.52, 33.83])
revenue_chengyao = np.array([2771, 3504, 3067, 3194])
growth_chengyao = np.array([5.56, 26.45, -12.47, 4.14])
revenue_yinpian = np.array([1275, 1297, 1237, 1467])
growth_yinpian = np.array([1.16, 1.73, -4.62, 18.59])

# 设置图表大小

plt.figure(figsize=(10, 6))

# 柱状图配置

bar_width = 0.5

index = np.arange(len(years))

# 第一个Y轴 - 营收相关

ax1 = plt.gca()  # 获取当前axes实例

ax1.bar(index, revenue_keli, bar_width, label='中药颗粒营收', color='b')
ax1.bar(index, revenue_chengyao, bar_width, label='中成药营收', color='g', bottom=revenue_keli)
ax1.bar(index, revenue_yinpian, bar_width, label='中药饮片营收', color='r', bottom=revenue_keli+revenue_chengyao)

# 设置第一个Y轴的范围和刻度

ax1.set_ylim(0, 30000)
ax1.set_yticks(np.arange(0, 30001, 5000))
# 添加虚线网格线

ax1.yaxis.grid(True, linestyle='--', linewidth=0.7, color='gray')  # 添加虚线


ax1.set_xlabel('年份')
ax1.set_ylabel('营收(百万元)')
ax1.set_xticks(index)
ax1.set_xticklabels(years)

# 第二个Y轴 - 同比增速

ax2 = ax1.twinx()  # 创建第二个axes实例

line_width = 3

ax2.plot(years, growth_keli, marker='o', markersize=10, linewidth=line_width, label='中药颗粒同比增速', color='c')
ax2.plot(years, growth_chengyao, marker='s', markersize=10, linewidth=line_width, label='中成药同比增速', color='m')
ax2.plot(years, growth_yinpian, marker='^', markersize=10, linewidth=line_width, label='中药饮片同比增速', color='y')

# 设置第二个Y轴的范围和刻度

ax2.set_ylim(-20, 60)
ax2.set_yticks(np.arange(-20, 61, 20))

# 为数据点添加数值标签

for i, txt in enumerate(growth_keli):
    ax2.annotate(f'{txt}%', (years[i], growth_keli[i]), textcoords="offset points", xytext=(0,10), ha='center')
for i, txt in enumerate(growth_chengyao):
    ax2.annotate(f'{txt}%', (years[i], growth_chengyao[i]), textcoords="offset points", xytext=(0,10), ha='center')
for i, txt in enumerate(growth_yinpian):
    ax2.annotate(f'{txt}%', (years[i], growth_yinpian[i]), textcoords="offset points", xytext=(0,10), ha='center')

ax2.set_ylabel('同比增速(%)')

# 添加图例

ax1.legend(loc='upper left')
ax2.legend(loc='upper right')

# 设置图表标题

plt.title('图3.2018-2021年多种中药品类的营收和增长情况')

# 显示图表

plt.show()

\

# 需求5
from pyecharts import options as opts
from pyecharts.charts import Line

#中文
plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False

categories = ["化学药", "中成药", "生物制品", "中药颗粒", "中药饮片", "保健品"]
ynby_sales = [33, 45, 10, 9, 4, 4]
trt_sales = [16, 14, 6, 8, 30, 33]

line_chart = Line()

line_chart.add_xaxis(categories)
line_chart.add_yaxis(
    "云南白药", 
    ynby_sales,
    markpoint_opts=opts.MarkPointOpts(
        data=[opts.MarkPointItem(type_="max", name="最大值")]
    )
)
line_chart.add_yaxis(
    "同仁堂",
    trt_sales,
    markpoint_opts=opts.MarkPointOpts(
        data=[opts.MarkPointItem(type_="max", name="最大值")]
    )
)

line_chart.set_global_opts(title_opts=opts.TitleOpts(title="两家公司各种品类药物销售额(柱形图)"))

line_chart.render_notebook()

from pyecharts import options as opts

from pyecharts.charts import Line


categories = ["化学药", "中成药", "生物制品", "中药颗粒", "中药饮片", "保健品"]
ynby_sales = [33, 45, 10, 9, 4, 4]
trt_sales = [16, 14, 6, 8, 30, 33]

line_chart = Line()
line_chart.add_xaxis(categories)
line_chart.add_yaxis(
    "云南白药",
    ynby_sales,
    markpoint_opts=opts.MarkPointOpts(
        data=[opts.MarkPointItem(type_="max", name="最大值")]
    )
)
line_chart.add_yaxis(
    "同仁堂",
    trt_sales,
    markpoint_opts=opts.MarkPointOpts(
        data=[opts.MarkPointItem(type_="max", name="最大值")]
    )
)

# 设置全局选项

line_chart.set_global_opts(
    title_opts=opts.TitleOpts(title="两家公司各种品类药物销售额", subtitle="云南白药和同仁堂"),
    yaxis_opts=opts.AxisOpts(name="销售额(亿元)", name_rotate=90, position="left"),  # Y轴标题竖直排列并且放到左侧

    xaxis_opts=opts.AxisOpts(name="药品种类")
)

line_chart.render_notebook()

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值