这篇文章主要介绍了python基于plotly实现画饼状图代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
代码
import pandas as pd
import numpy as np
import plotly.plotly as py
import plotly.graph_objs as go
path = "/home/v-gazh/PycharmProjects/us_data/limit_code.csv"
df = pd.read_csv(path)
df.set_index(["code"], inplace=True)
# ST 占比
total_count = len(df)
st_count = len(df[df["isST"]==1])
print(f"禁投池总数:{total_count}")
print(f"禁投池中ST个数:{st_count}") # f"禁投池中ST个数:{}"
# 成分股占比
sz50_count = len(df[df["isSz50"]==1])
print(f"禁投池中上证50个数:{sz50_count}")
hs300_count = len(df[df["isHs300"]==1])
print(f"禁投池中沪深300个数:{hs300_count}")
zz500_count = len(df[df["isZz500"]==1])
print(f"禁投池中中证500个数:{zz500_count}")
# 退市占比
outdate_count = len(df["outDate"].dropna())
print(f"禁投池中退市股票个数:{outdate_count}")
# 非股票
not_stock = len(df[df["type"]!=1])
print(f"禁投池中非股票个数:{not_stock} 【SZ006415 为基金:F006415 | SZ000000 代码错误】")
# 次新股
delta_df = pd.DataFrame((pd.to_datetime(df["date"]) - pd.to_datetime(df["ipoDate"])))
new_stock = len(delta_df[delta_df[0] < pd.Timedelta("365 days")]) # 上市不满一年为次新股
print(f"禁投池中次新股个数:{new_stock}")
# 市值小于30亿的股票
maketValue = len(df[df["maketValue"] < 3000000000])
print(f"市值小于30亿股票个数:{maketValue}")
# 画图
labels = ["股票总数", "ST股票", "深证50", "沪深300", "中证500", "退市股票", "非股票", "次新股", "小市值"]
values = [total_count, st_count, sz50_count, hs300_count, zz500_count, outdate_count, not_stock, new_stock, maketValue]
trace = go.Pie(labels=labels, values=values,textfont=dict(size=15),)
py.iplot([trace], filename="basic_pie_chart")
注:上面代码中,起主要作用的主要是
# 画图
labels = ["股票总数", "ST股票", "深证50", "沪深300", "中证500", "退市股票", "非股票", "次新股", "小市值"]
values = [total_count, st_count, sz50_count, hs300_count, zz500_count, outdate_count, not_stock, new_stock, maketValue]
trace = go.Pie(labels=labels, values=values,textfont=dict(size=15),)
py.iplot([trace], filename="basic_pie_chart")
values = [total_count, st_count, sz50_count, hs300_count, zz500_count, outdate_count, not_stock, new_stock, maketValue]
values 列表里的内容为int数值,对应上面的labels
图示
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持云海天教程。