Draw a "Lollipop chart" with Excel and python

Data Studio 2021-09-15 07:24:22
draw lollipop chart excel python

Hello everyone , I am cloud king !

Today I'll bring you an interesting Visualization —— Lollipop chart Detailed drawing tutorial . contrast Excel And Pyhton, Teach you to draw a lollipop chart on the tall with your hands .

Excel Draw a lollipop chart

The first is data preparation , First copy the original data to a column , Get two columns of the same data .

Select data , Insert composite diagram , Set the combination chart of histogram and scatter chart respectively , After confirmation, you can get a preliminary lollipop chart .

You can set the corresponding style as required :

Which set x The position of the shaft is relatively complex .

First select y Axis , Right click and then click Format axis , Next, set the abscissa position and abscissa label respectively . Set up x The axis value is 0.8, Or the axis you need . Set up x The axis label is at the bottom of the diagram .

The result is the following pattern .

Of course , You can set the style of bars and scatter points , Such as color, shape, etc . You can also select the style of a bar chart and scatter mark separately , To highlight one or more values .

Python Draw a lollipop chart

Bar+Scatter Combination method

The first method , and Excel The same way of thinking , Use a bar chart (plt.bar) And scatter plot (plt.scatter) Composite drawing . If you want a different style , You can style bar charts and scatter charts separately , To change the overall style .

n = len(values)
colors1 = ["red"]+((n-1)*["olive"])
colors2 = ["red"]+((n-1)*["blue"])
plt.rcParams['figure.figsize'] = (23.5, 10)
plt.bar(values.index, values.values,
color=colors1, # Set the line color
width=0.05) # Set line width
plt.scatter(values.index, values.values,
color=colors2,# Set the marker point width
s=50) # Set marker point size

Set the style

This step mainly realizes X Move up axially .

  • Will all y Value plus a fixed value v( negative ):y = y+v
  • And set up y The lower limit of the axis is the fixed value v :plt.ylim(v, 0.6)

Draw a horizontal line

  • Draw a baseline by drawing a polyline , And fix it y=0 plt.plot(values.index, [0]*n)
  • Or draw the baseline directly with the linear function , Same settings y=0 plt.axhline(y=0, xmin=0, xmax=1)
  • The above two methods can draw a straight line diagram , The disadvantage of the second method is that when setting the two ends of the line , It's not easy to control , because xmin and xmax The set value is in 0~1 The proportional value between . The second method can directly set the point values at both ends of the polyline .
# plt.style.use('dark_background') # Set the black background
plt.rcParams['figure.figsize'] = (23.5, 10)
plt.rcParams['axes.unicode_minus'] = False
v = -0.8
plt.bar(values.index, values.values+v,
plt.scatter(values.index, values.values+v,
# plt.plot(values.index,[0]*n,color='darkblue')
plt.axhline(y=0,xmin=0.045, xmax=0.955,
color='darkblue' ,

Stem Function method

stay python in , We make use of matplotlib.pyplot.stem You can draw a lollipop chart directly ( Stem and leaf ). This function only needs one line of code to draw a lollipop chart . Let's take a look at the magical plt.stem() Main parameters of .

stem(x,y, linefmt=None, markerfmt=None, basefmt=None) x : array-like, optional data x Axis y : array-like data y Axis linefmt : str, optional You can use color and linetype to set the color and type of vertical lines , Such as 'r-', A solid red line . markerfmt : str, optional Set the type and color of vertices , The default value is :'C0o',C( Capital C) By default , The last number should be 0-9, Color change , final . perhaps o( Lowercase letters o) The vertices can be set as small real points or large real points respectively . basefmt : str, default: 'C3-' ('C2-' in classic mode) Defines the format string of the baseline attribute . Namely y=0 The right line

In terms of parameters , Use its parameters to set the color , You can only use... In the color circle set by this function 0-9 Number color . If you want to set other colors , You can use plt.setp To set the line style .

 n = len(values2)
colors1 = ["red"]+((n-1)*["olive"])
(markers, stemlines, baseline) = plt.stem(values2, markerfmt="C0o")
linewidth=0.5 ) # Specify the properties of each row
linewidth=2 )

By setting parameters bottom=0.8 To adjust the baseline position , And set the background style .

(markers, stemlines, baseline) = plt.stem(values2,
plt.setp(stemlines, linestyle="-",
color="y", linewidth=2 )

Finally, when there is too much data , The effect of drawing a lollipop chart .

OK, Today's sharing is here !

I didn't see enough. ? Like watching Walk up ~ More exciting follow-up ~

This article is from WeChat official account. - data STUDIO(jim_learning) , author : Cloud King

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the yunjia_community@tencent.com Delete .

Original publication time : 2021-08-24

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

本文为[Data Studio]所创,转载请带上原文链接,感谢

  1. Take you to learn more about nginx basic login authentication: generating passwords using Python
  2. 超硬核Python避坑学习方案奉上!入门到就业一篇就搞定!
  3. Talk about how JMeter executes Python scripts concurrently
  4. Talk about how JMeter executes Python scripts concurrently
  5. Talk about how JMeter executes Python scripts concurrently
  6. python内置函数通过字符串的方式来执行函数代码块,类似java的反射机制相当强大!
  7. python内置函数通过字符串的方式来执行函数代码块,类似java的反射机制相当强大!
  8. python內置函數通過字符串的方式來執行函數代碼塊,類似java的反射機制相當强大!
  9. Les fonctions intégrées Python exécutent des blocs de code de fonction à travers des chaînes, et les mécanismes de réflexion comme Java sont assez puissants!
  10. Python module 1
  11. Python tip: use namedtuple instead of manually created classes
  12. Python - poetry(3)配置项详解
  13. Python - poetry(3)配置项详解
  14. Python - poetry(3)配置項詳解
  15. Python - poetry(3)配置項詳解
  16. Python - détails de l'élément de configuration Poetry (3)
  17. Python - détails de l'élément de configuration Poetry (3)
  18. Python案例实战,pygame模块,Python实现字母代码雨
  19. Python calculation vector angle code
  20. Python基础面试题解读|《Python面试100层》|第1层
  21. 面对小白的pandas命令手册+练习题【三万字详解】
  22. 面對小白的pandas命令手册+練習題【三萬字詳解】
  23. Face au Manuel de commande pandas de Xiaobai + question d'exercice [30 000 mots pour plus de détails]
  24. Interprétation des questions d'entrevue de base Python | 100 couches d'entrevue Python | couche 1
  25. Python data structure and algorithm (17) -- merge sort
  26. Les fonctions intégrées Python exécutent des blocs de code de fonction à travers des chaînes, et les mécanismes de réflexion comme Java sont assez puissants!
  27. Python笔记-uiautomator2截图点击,OpenCV找图
  28. Python文档阅读笔记-OpenCV中Template Matching
  29. Python笔记-利用OpenCV的matchTemplate屏幕找图并使用pyautogui点击
  30. Python筆記-利用OpenCV的matchTemplate屏幕找圖並使用pyautogui點擊
  31. Notes python - utilisez l'écran matchtemplate d'OpenCV pour trouver des images et cliquez sur
  32. Notes de lecture de documents python - Matching de modèles dans OpenCV
  33. Notes python - capture d'écran de l'automate 2 Cliquez pour ouvrir la vue
  34. python链接云服务器的mysql8
  35. python鏈接雲服務器的mysql8
  36. Mysql8 pour les serveurs Cloud liés Python
  37. Python资源大集合,要的话可以拿走!
  38. ️万字【Python基础】保姆式教学️,小白快速入门Python!
  39. ️萬字【Python基礎】保姆式教學️,小白快速入門Python!
  40. Wanzi [base Python] Baby - sitting Teaching, Little White Quick Start Python!
  41. Realizing the function of sending e-mail automatically with Python
  42. Smtpauthenticationerror in Python: solution
  43. 8 steps to teach you how to solve Sudoku in Python! (including source code)
  44. Python change la vie | identifier facilement des centaines de numéros de livraison
  45. Python change life | utilisation de modèles reconnus par ocr
  46. Bibliothèques Python utiles et intéressantes - - psutil
  47. 3. Traitement des données pandas
  48. 【Python编程基础】控制流之链式比较运算符
  49. MFC uses Python scripting language
  50. 【Python編程基礎】控制流之鏈式比較運算符
  51. 【 base de programmation python】 opérateur de comparaison de chaîne pour le flux de contrôle
  52. Python game development, pyGame module, python implementation of Xiaole games
  53. Mise en œuvre du Code de vérification unique (OTP) avec le cadre de repos Django
  54. Python - eval ()
  55. Python - Programmation orientée objet - _Rapport()
  56. Différence entre python - rep (), Str ()
  57. Python - Programmation orientée objet - _Appel()
  58. Python calling matlab script
  59. Python - Programmation orientée objet - _Nouveau() et mode Singleton
  60. Python - Programmation orientée objet - méthode magique (méthode de double soulignement)