In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces how to use python to draw column chart related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will have something to gain after reading this article on how to draw a column chart with python, let's take a look at it.
# column chart import pandasimport numpyimport matplotlib from matplotlib import pyplot as plt# import data data_columns=pandas.read_csv ('D://Python projects//reference data//6.4//data.csv') # define Chinese format font= {' family':'MicroSoft Yahei', 'weight':'bold',' size':12} matplotlib.rc ('font',**font) # use mobile phone brand as grouping column Monthly consumption as a statistical column result_columns=data_columns.groupby (by= ['mobile phone brand'], as_index=False) ['monthly consumption (yuan)'] .agg ({'monthly total consumption': numpy.sum}) # generates a sequence index=numpy.arange with an interval of 1 (result_columns. Total monthly consumption. Size) # draw a vertical bar chart plt.bar (index,result_columns ['monthly total consumption']) #% matplotlib qtplt.show () # configure color maincolor= (42go 256277) plt.bar (index,result_columns ['monthly total consumption']) plt.show () # configure X-axis label plt.bar (index,result_columns ['monthly total consumption']) plt.xticks (index,result_columns. Mobile brand) plt.show () # sorts the data in descending order and displays result_asd=result_columns.sort_values (by=' monthly consumption', ascending=False) plt.bar (index, result_asd. Total monthly consumption, color=maincolor) plt.xticks (index,result_asd. Mobile phone brand) plt.show ()
The result is:
# horizontal bar chart result_asd=result_columns.sort_values (by=' monthly total consumption', ascending=False) plt.barh (index, result_asd. Total monthly consumption, color=maincolor) plt.yticks (index,result_asd. Mobile phone brand) plt.show ()
The result is:
# calculate the data result=data_columns.pivot_table of crosstab (values=' monthly consumption (CNY)', index=' mobile brand', columns=' communication brand', aggfunc=numpy.sum)
The result is:
# define three colors index=numpy.arange (len (result)) mincolor= (42x256) plt.bar (index+1/4, result) plt.bar (index+1/4, result ['dynamic zone'], color=midcolor Width=1/4) plt.bar (index+1/2, result ['Shenzhouxing'], color=maxcolor, width=1/4) plt.xticks (index+1/3,result.index) # add legend plt.legend (['GSM', 'dynamic Zone', 'Shenzhouxing']) plt.show ()
The result is:
# reorder to draw result=result.sort_values (by=' Shenzhouxing', ascending=False) plt.bar (index, result ['GSM'], color=mincolor, width=1/4) plt.bar (index+1/4, result ['dynamic zone'], color=midcolor, width=1/4) plt.bar (index+1/2 Result ['Shenzhouxing'], color=maxcolor, width=1/4) plt.xticks (index+1/3,result.index) plt.legend (['GSM', 'dynamic Zone', 'Shenzhouxing']) plt.show ()
The result is:
# draw stacked bar chart result=result.sort_values (by=' Shenzhouxing', ascending=False) plt.bar (index, result ['GSM'], color=maxcolor) plt.bar (index, result ['dynamic Zone'], bottom=result ['GSM'], color=midcolor) plt.bar (index, result ['Shenzhouxing'] Bottom=result ['GSM'] + result ['dynamic zone'], color=mincolor) plt.xticks (index,result.index) plt.legend (['GSM', 'dynamic zone', 'Shenzhouxing']) plt.show ()
The result is:
# draw bi-directional bar chart plt.barh (index, result ['Shenzhouxing'], color=midcolor) plt.barh (index,-result ['dynamic zone'], color=maxcolor) plt.yticks (index, result.index) plt.legend (['dynamic zone', 'Shenzhouxing']) plt.show ()
The result is:
This is the end of the article on "how to draw a column chart with python". Thank you for reading! I believe you all have a certain understanding of "how to draw a bar chart with python". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.