2019年大数据网络赛 第四部分:可视化 柱线混合图:twinx()

第三问:

利用柱线混合图形画出合肥市,高新区,庐阳区的pm2.5的值。要求将输出的折线图保存成图像文件,X轴表示时间,柱状表示 高新区 的pm2.5的值,线形表示 庐阳区的pm2.5的值。(5分)

要求:图形中含有图例

说明:此题主要利用了twinx()将柱形图与折线图进行连接放在一起

ax1 = fig.add_subplot(111)

ax2 = ax1.twinx()

代码如下:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas import Series,DataFrame

df = pd.read_csv("new1")
plt.rcParams['font.sans-serif']=['SimHei']
fig = plt.figure(figsize=(10,6))

s1 = df["PM2.5指数"].loc[(df["城市"]=="合肥")&(df["区域"]=="高新区")]
s2 = df["PM2.5指数"].loc[(df["城市"]=="合肥")&(df["区域"]=="庐阳区")]
list_name = ['2018-12-01','2018-12-02','2018-12-03','2018-12-04','2018-12-05','2018-12-06','2018-12-07','2018-12-08','2018-12-09','2018-12-10']

ax1 = fig.add_subplot(111)
ax1.bar(range(len(s1)),s1,tick_label=list_name,label="高新区")
ax1.set_ylabel("高新区的PM2.5值的变化")
# 柱线混合图的连接方式利用twinx(),通过设置ax1和ax2进行连接
ax2 = ax1.twinx()
ax2.plot(list_name,s2,color="red",marker="o",label="庐阳区")
ax2.set_ylabel("庐阳区的PM2.5值的变化")
plt.legend()

plt.title("柱线混合图形高新区,庐阳区的pm2.5的值")
plt.savefig("3.png")
plt.show()

图形如下:

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,那么我可以为您提供一些Python可视化库的使用方法来实现您的需求。 1. Matplotlib库 Matplotlib是一个用于创建静态、动态和交互式可视化Python库,可以绘制双折线、折线柱状混合等。 绘制双折线: ```python import matplotlib.pyplot as plt # 数据 x = [1,2,3,4,5] y1 = [1,3,5,3,1] y2 = [2,4,6,4,2] # 绘 plt.plot(x, y1, label='line 1') plt.plot(x, y2, label='line 2') # 添加标签和标题 plt.xlabel('x label') plt.ylabel('y label') plt.title("Double Line Graph") # 添加例 plt.legend() # 显示图像 plt.show() ``` 绘制折线柱状混合: ```python import numpy as np import matplotlib.pyplot as plt # 数据 x = [1,2,3,4,5] y1 = [1,3,5,3,1] y2 = [2,4,6,4,2] y3 = [5,5,5,5,5] # 绘 fig, ax1 = plt.subplots() ax1.plot(x, y1, 'g-', label='Line1') ax1.plot(x, y2, 'b-', label='Line2') ax1.set_xlabel('X Label') ax1.set_ylabel('Y Label') ax1.set_title('Line1 and Line2 Graph') ax1.legend(loc='upper left') ax2 = ax1.twinx() ax2.bar(x, y3, alpha=0.3, color='r', label='Bar') ax2.set_ylabel('Bar Label') ax2.legend(loc='upper right') plt.show() ``` 2. Seaborn库 Seaborn是一个基于Matplotlib的Python可视化库,提供了更高级别的界面和绘风格,可以绘制双折线、折线柱状混合等。 绘制双折线: ```python import seaborn as sns import matplotlib.pyplot as plt # 数据 x = [1,2,3,4,5] y1 = [1,3,5,3,1] y2 = [2,4,6,4,2] # 绘 sns.lineplot(x=x, y=y1, label='Line 1') sns.lineplot(x=x, y=y2, label='Line 2') # 添加标签和标题 plt.xlabel('X Label') plt.ylabel('Y Label') plt.title("Double Line Graph") # 添加例 plt.legend() # 显示图像 plt.show() ``` 绘制折线柱状混合: ```python import seaborn as sns import matplotlib.pyplot as plt # 数据 x = [1,2,3,4,5] y1 = [1,3,5,3,1] y2 = [2,4,6,4,2] y3 = [5,5,5,5,5] # 绘 fig, ax1 = plt.subplots() sns.lineplot(x=x, y=y1, ax=ax1, label='Line1') sns.lineplot(x=x, y=y2, ax=ax1, label='Line2') ax1.set_xlabel('X Label') ax1.set_ylabel('Y Label') ax1.set_title('Line1 and Line2 Graph') ax1.legend(loc='upper left') ax2 = ax1.twinx() sns.barplot(x=x, y=y3, ax=ax2, alpha=0.3, color='r', label='Bar') ax2.set_ylabel('Bar Label') ax2.legend(loc='upper right') plt.show() ``` 希望这些代码可以帮助您实现您的需求!
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值