How to use subgraph to draw data in Matplotlib
Today, I will talk to you about how to use subgraphs to draw data in Matplotlib. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
First, we read the data from the data.csv file and draw it:
Let's take a look at the code implementation: import pandas as pdfrom matplotlib import pyplot as pltfrom matplotlib import font_manager
Plt.style.use ('seaborn')
Data = pd.read_csv ('data.csv') ages = data [' Age'] dev_salaries = data ['All_Devs'] py_salaries = data [' Python'] js_salaries = data ['JavaScript'] # create subgraph fig, (ax1,ax2) = plt.subplots (nrows=2,ncols=1) ax1.plot (ages, dev_salaries, color='#444444', linestyle='--', label='All Devs') ax2.plot (ages, py_salaries, label='Python') ax2.plot (ages Js_salaries, label='JavaScript') zh_font = font_manager.\ FontProperties (fname='C:\\ Windows\\ Fonts\ msyh.ttf')
Ax1.legend () ax1.set_title ('average salary chart of age', fontproperties=zh_font) ax1.set_ylabel ('average salary', fontproperties=zh_font)
Ax2.legend () ax2.set_xlabel ('age', fontproperties=zh_font) ax2.set_ylabel ('average salary', fontproperties=zh_font)
Plt.tight_layout ()
Plt.show () # Save the chart as a picture fig.savefig ('fig.png')
Running result:
After reading the above, do you have any further understanding of how to use subgraphs to draw data in Matplotlib? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.