首页 > 编程语言 >python-绘图进阶

python-绘图进阶

时间:2022-11-01 17:01:25浏览次数:37  
标签:set 进阶 title python sun plt 绘图 ax stock

数据准备

import matplotlib.pyplot as plt
import tushare as ts
import pandas as pd
import datetime
%matplotlib inline
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
plt.rcParams['axes.unicode_minus'] = False # 解决负数无法正常显示的问题
plt.style.use('ggplot') # 修改图表样式
# 获取2021-01-01至今的股票数据
sun_stock = ts.get_k_data(code='002078', start='2021-01-01', end=datetime.datetime.now().strftime('%Y-%m-%d'))
sun_stock.head()

sun_stock.describe()

# 将date设置为数据的index
sun_stock.set_index('date',inplace=True) 

绘制折线图

# 绘图
ax = sun_stock.plot()
ax.set_title('sun_stock')
plt.show()

拆分显示多个子图

# 通过subplots拆分显示折线图
ax1, ax2, ax3, ax4, ax5 = sun_stock.plot(subplots=True)
ax1.set_title('subplot1')
ax2.set_title('subplot2')
ax3.set_title('subplot3')
ax4.set_title('subplot4')
ax5.set_title('subplot5')
plt.show()

# 添加第二个y轴,如何给第二个y轴set_ylabel ???
ax = sun_stock[['open','high']].plot(secondary_y=['high'], figsize=(20,7))
ax.set_ylabel('开盘价')
plt.show()

绘制柱状图

# 创建数据
boolean=[True,False]
gender=["男","女"]
color=["white","black","yellow"]
df=pd.DataFrame({
    "height":np.random.randint(150,190,100),
    "weight":np.random.randint(40,90,100),
    "smoker":[boolean[x] for x in np.random.randint(0,2,100)],
    "gender":[gender[x] for x in np.random.randint(0,2,100)],
    "age":np.random.randint(15,90,100),
    "color":[color[x] for x in np.random.randint(0,len(color),100) ]
}
)

ax = df[['smoker','weight']].groupby(['smoker']).mean().plot.bar(figsize=(5,7), title='是否吸烟与平均体重关系')
ax.legend(bbox_to_anchor=(1,1))
# ax.set_title('是否吸烟与平均体重关系')
plt.show()

绘制累积柱形图

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [6, 10, 4, 5, 1]
y1 = [2, 6, 3, 8, 5]

plt.bar(x, y, align="center", color="#66c2a5", tick_label=["A", "B", "C", "D", "E"], label="班级A")
plt.bar(x, y1, align="center", bottom=y, color="#8da0cb", label="班级B")

plt.xlabel("测试难度")
plt.ylabel("试卷份数")

plt.legend()

plt.show()

标签:set,进阶,title,python,sun,plt,绘图,ax,stock
From: https://www.cnblogs.com/wxyz94/p/15582030.html

相关文章

  • Windows下Git Bash运行python的三种办法
    以前运行cmd命令都是在cmd里面的,但是那个页面实在是太丑了,后面我就全部用gitbash来运行window下的命令了。但是在gitbash中运行下python–-version或piplist命令......
  • Python发送QQ邮件
    Python发送QQ邮件1、登陆QQ邮箱,获取授权码可以参考官网说明登录QQ邮箱点击设置点击账户、点击开启POP3/SMEP服务点击开启后验证密保,然后根据操作发送短信......
  • python 二维码检测-
    参考文章:使用微信扫一扫二维码接口解密QRcode-知乎(zhihu.com) importcv2importnumpyasnpimportosdefopen_img(img_dir):img_list=[]for_,......
  • 关于Python封装函数的几道练习题
    1.封装函数,可以判断一个数字是否为偶数deffunc(n):ifn%2==0:print("%d是偶数"%n)else:print("%d是奇数"%n)func(11)#11是奇数2.封装......
  • python基础复习
    目录今日内容概要管理员功能说明及建议总复习函数模块homework今日内容概要管理员功能说明及建议1、冻结账户2、删除账户3、查看/修改指定用户各项数据(密码、购物车)......
  • vs2013配置python 安装第三方工具包
    这里以matplotlib安装为例。选择pip搜索camelcase进行安装。   ......
  • vs2013配置python_vs2013如何安装python
    vs2013如何安装python?步骤如下:1、安装PTVS:下载PTVS①找到下图位置,下载PythonToolsforVS2013地址:https://github.com/Microsoft/PTVS/releases/v2.2.2 ②安装RT......
  • PG plpython存储过程计算结果直接入库
    >处理函数(返回多条结果数组)dropFUNCTIONcal_charge_sample(recordsdwd_pv_behavior_di[]);CREATEFUNCTIONcal_charge_sample(recordsdwd_pv_behavior_di[])R......
  • Python 变量作用域
    一、Python中变量作用域分为以下四种,简称LEGB:Local局部变量Enclosed嵌套变量Global全局变量Built-in内置变量Local局部变量:暂时的存在,依赖于创建该局部作用......
  • 拓端tecdat|python代写辅导虎扑社区论坛数据爬虫分析报告
    以下是摘自虎扑的官方介绍:虎扑是为年轻男性服务的专业网站,涵盖篮球、足球、F1、NFL等赛事的原创新闻专栏视频报道,拥有大型的生活/影视/电竞/汽车/数码网上交流社区,聊体育谈......