treemap8.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import plotly_express as px
  2. import pandas as pd
  3. import plotly
  4. # Read data from excel
  5. df = pd.read_csv("../docs/keyword.csv")
  6. keyword = df['Keyword']
  7. # competition = df['Competition (indexed value)']
  8. bid = df['Top of page bid (low range)']
  9. search = df['Avg_monthly_searches']
  10. margin = df['Top of page bid (low range)']
  11. remark = df['Keyword']
  12. # 預處理刪掉不要的data
  13. # # Create chart and store as figure [fig]
  14. # fig = px.sunburst(df,
  15. # path=[keyword, bid],
  16. # values=search,
  17. # color=margin,
  18. # color_continuous_scale=['green', 'yellow', 'red'],
  19. # title='Keyword Overview',
  20. # hover_name=remark
  21. # )
  22. # Create chart and store as figure [fig]
  23. fig = px.treemap(df,
  24. path=[keyword, bid],
  25. values=search,
  26. color=margin,
  27. # color_continuous_scale=['green', 'yellow', 'red'],
  28. color_continuous_scale=px.colors.sequential.Peach,
  29. title='Keyword Overview',
  30. hover_name=remark,
  31. )
  32. fig.update_traces(root_color="lightgrey")
  33. # Update/change layout
  34. fig.update_layout(title_font_size=42,
  35. title_font_family='Arial',
  36. margin=dict(t=50, l=25, r=25, b=25)
  37. )
  38. fig.show()
  39. # save chart and export to HTML
  40. plotly.offline.plot(fig, filename='../chart.html')