我有一个数据集,其中的一小部分如下所示,
data = [ ['2018-01-01', 1.323 , 'AI' , 2000,'Communications','Mothers'],
['2018-01-02', 1.525 , 'AI', 1500,'Communications','Mothers'],
['2018-01-03', 1.045 , 'AI' , 500,'Communications','Mothers'],
['2018-01-04', 1.845 , 'AI' , 600,'Communications','Mothers'],
['2018-01-05', 1.045 , 'AI' , 500,'Communications','Mothers'],
['2018-01-02', 1.446 , 'BOC' , 550,'Pharmaceuticals','JASDAQ Standard'],
['2018-01-03', 2.110 , 'BOC' , 3201,'Pharmaceuticals','JASDAQ Standard'],
['2018-01-04', 2.150 , 'BOC' , 5200,'Pharmaceuticals','JASDAQ Standard'],
['2018-01-05', 2.810 , 'BOC' , 1980,'Pharmaceuticals','JASDAQ Standard'],
['2018-01-03', 5.199 , 'CAT' , 2000,'Real Estate','Mothers'],
['2018-01-06', 4.980 , 'CAT' , 450,'Real Estate','Mothers'],
['2018-01-07', 4.990 , 'CAT' , 3000,'Real Estate','Mothers']]
df = pd.DataFrame(data,columns =['date', 'price', 'ticker', 'volume', 'Sector','Market Division'])我想要显示哪个市场部门有更多的库存,来自哪个部门。我尝试了下面的树状地图,但没有工作,我怎么做呢?
import plotly.express as px
import numpy as np
a=df.groupby(['Market Division','Sector']).count()
a["Exchange"] = "Exchange" # in order to have a single root node
fig = px.treemap(a, path=['Exchange', 'Market Division', 'Sector','ticker'], values='ticker')
fig.show()发布于 2021-05-04 12:56:10
您可以尝试使用stacked plots。下面是一个虚拟示例:
import matplotlib.pyplot as plt
labels = list(set([md for md in df['Market Division']]))
fig, ax = plt.subplots()
jasdaq = [3434, 5454, 45454]
mothers = [35345, 64534, 43543]
ax.bar(labels, jasdaq[0], label='Pharmaceuticals')
ax.bar(labels, jasdaq[1], label='Communication')
ax.bar(labels, jasdaq[2], label='Real Estate')
ax.bar(labels, mothers[0], label='Pharmaceuticals')
ax.bar(labels, mothers[1], label='Communication')
ax.bar(labels, mothers[2], label='Real Estate')
ax.legend()
plt.show()

您需要首先计算每个Market分区的每个扇区,并替换jasdaq和mothers,以获得您想要的真实图。
https://stackoverflow.com/questions/67378790
复制相似问题