Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to populate the Line Graph in Matplotlib

Shulou Source: shulou.com Published: 2022-06-01 18:37:50 10月03日 Update

This article will explain in detail how to fill in the diagram in Matplotlib. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Background introduction

You will learn how to populate the line graph in Matplotlib. Not only does this make our chart look more professional, but we can also add useful information by filling areas based on specific thresholds.

Getting started example

Next, let's take a look at an example: read a data.csv file as a medium company table that counts all developers, Python developers, and JavaScript developers of different ages. We use a filled area to show that the salary age of Python developers is higher than that of all developers (yellow area), as well as the age of salaried people below all developers (red area). The csv file is roughly as follows:

The specific code is as follows:

Import pandas as pdfrom matplotlib import pyplot as pltfrom matplotlib import rcParams# sets the chart font to prevent Chinese garbled rcParams ['font.family'] =' Microsoft YaHei'rcParams ['font.sans-serif'] =' Microsoft YaHei'

Data = pd.read_csv ('data.csv') ages = data [' Age'] dev_salaries = data ['All_Devs'] py_salaries = data [' Python'] js_salaries = data ['JavaScript']

Plt.plot (salary for all ages,dev_salaries,color='#444444', linestyle='--',label=' developers') plt.plot (salary for ages,py_salaries,label='Python developers) # use the fill_between () method to populate the area # where when the python development salary is greater than all development salaries # interpolate defines the fill area as Ture#color: area color # alpha: set transparency plt.fill_between (ages,py_salaries, dev_salaries Where= (py_salaries > dev_salaries), interpolate=True,color='yellow',alpha=0.25, label=' above the specified salary') # use the fill_between () method to fill the area # where when the python development salary is less than or equal to all development salaries # interpolate defines the fill area as Ture#color: area color # alpha: set transparency plt.fill_between (ages,py_salaries,dev_salaries, where= (py_salaries)

Tags: Development salary region personnel age line map content article chart salary file method time more transparency color different good practical professional Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Docker Apple Shulou Tech Info Redmi