Get the App
SLTechnology News&Howtos  ›  Development  › 

How to draw the bar chart of python data visualization

Shulou Source: shulou.com Published: 2022-05-31 20:30:27 10月04日 Update

This article "python data visualization bar chart how to draw" the knowledge points of most people do not understand, so the editor summarized the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "python data visualization bar chart how to draw" article.

What is a bar chart?

A bar chart (bar chart) is a graph that represents the amount of data in terms of the height or length of bars of the same width. Bar charts can be horizontally or vertically, also known as column chart. In addition, the bar chart has the form of simple bar chart, compound bar chart and so on.

To put it simply, the width of a bar chart is generally the same, and the height or length of the bar represents the amount of data, which is the essential difference between a bar chart and a histogram.

The first method of painting

Import numpy as npfrom pandas import DataFrame# because the scale value on our x-axis is in Chinese, we need to use this package to display Chinese from matplotlib.pyplot import rcParams# display Chinese kaiti for italics rcParams ['font.sans-serif'] =' kaiti'# bar chart (vertical) df = DataFrame (data=np.random.randint (50 ~ 100) Si = (3)), index= ['Zhang San','Li Si', 'Wang Wu'] The running result of columns= ['Python','En','Math']) df.plot (kind='bar',fontsize=20) # is as follows:

The second method of painting

Import numpy as npimport matplotlib.pyplot as plt%matplotlib inlinex = ['Zhang San','Li Si', 'Wang Wu'] height = np.random.randint (80Permian 100recorder sizetal3) plt.bar (xlemagne HeightWidthpiece 0.2) height = np.random.randint (50Leijie 80pr sizeword3) plt.bar (xgramme HeightWidthpiece 0.2) height = np.random.randint (10LJ80) plt.bar (xheight) Width=0.2) # setting legend ncol indicates that one line shows the position of three legend loc setting legend plt.legend (['math score', 'Python score', 'English score'], ncol=3,loc= (0Pol 1)) # the running result is as follows:

The third method of painting

Using pyecharts,pyecharts is a class library for generating Echarts diagrams. Echarts is an open source data visualization JS library of Baidu. The visualization effect of the graph generated by Echarts is very good. In order to dock with Python and facilitate the direct use of data generation map in Python, it is simple and convenient, and the visualization effect is great. Let's take a look at it.

Import numpy as npfrom pyecharts.charts import Barfrom pyecharts import options as opts# V1 version began to support chained calls bar = (Bar () .add _ xaxis (['Zhang San','Li Si', 'Wang Wu']) # it should be noted here that what is passed on the y axis can only be a list, not an array. If the array data cannot be displayed, .add _ yaxis ("python score", np.random.randint (40pint 100recorder sizesheet 3). Tolist ()) .add _ yaxis ("maths score", np.random.randint (40pint 100Powersizesheet 3). Tolist ()) .add _ yaxis ("English grade", np.random.randint (40min100 minise3). Tolist ()) .set _ global_opts (title_opts=opts.TitleOpts (title= "bar chart of junior students' grades in a university"). Subtitle='K class')) # if you are not accustomed to chained calls, you can use the regular operation''bar = Bar () bar.add_xaxis ([' Zhang San','Li Si', 'Wang Wu']) bar.add_yaxis ("python score", np.random.randint (40 and 100 tolist) bar.add_yaxis ("Mathematics", np.random.randint (40 and 100)) bar.add_yaxis ("English score") Np.random.randint (40Jing 100Jing sizekeeper 3). Tolist () bar.set_global_opts (title_opts=opts.TitleOpts (title= "Grade bar chart for junior students of a certain university, subtitle='K class"))''# output bar.render_notebook () # on jupyter notebook. It can also be rendered to the local html file # bar.render ('. / grade .html') # the running result is as follows:

The above is about "how to draw the bar chart of python data visualization". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Tags: Bar shape score data visualization content Zhang San Li Si Wang Wu legend mathematics drawing result Chinese English generation operation same university student width Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MySQL Redmi Huawei Shulou Tech Info