Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python matplotlib to realize Line Chart

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use Python matplotlib to achieve a broken line chart". In daily operation, I believe many people have doubts about how to use Python matplotlib to achieve a broken line chart. Xiaobian consulted all kinds of materials and sorted out simple and easy to use operation methods. I hope to help you answer the doubts about "how to use Python matplotlib to achieve a broken line chart"! Next, please follow the small series to learn together!

I. Version # 01 matplotlib installation import matplotlib matplotlib.__ version__II. Chart theme setting

Please click: Chart Theme Settings

import numpy as np from matplotlib import pyplot as plt #How to use Chinese title plt.rcParams <$'font.sans-serif']=<$'Microsoft YaHei']#Use Microsoft Yahei font x = np.arange(1,11) y = 2 * x + 5 #The picture shows this formula plt.title("Matplotlib display") plt.xlabel("x axis") plt.ylabel("y axis") plt.plot(x,y) plt.show ()

IV. Multiple linear functions

Create a chart about the movie box office:

films="'Embrace You Through Winter,'' G Storm (G) 5: Final Chapter','Li Mao Plays Crown Price',' Mistake 2','Love by Years',' Matrix: Matrix Restart ',' Lion Teen','Magic Full House',' Team Wang makes great contributions to big movie','Love Myth'] regions=[' China','Britain',' Australia ',' US','US',' China','Britain',' Australia ',' US','US'] bos=[' 61,181','44,303',' 42,439','22,984',' 13,979','61,181','44,303','41,439','20,984','19,979']persons=['31','23','56','17','9','31','23','56','17','9']prices=['51','43','56','57','49','51','43','56','57','49']showdate=['2022-12- 03','2022-12- 05','2022-12- 01','2022 -12- 02','2022-11- 05','2022 -12- 03','2022 -12- 05','2022-12- 01','2022 - 12- 02',' 2022-11- 05'] ftypes=['story','Action',' Comedy ',' Drama','Drama',' Love','Action',' Animation'] points=['8.1',' 9.0','7.9',' 6.7','3.8',' 8.1','9.0',' 7.9','6.7',' 3.8'] filmscript ={ 'ftypes':ftypes, 'bos':bos, 'prices':prices, 'persons':persons, 'regions':regions, 'showdate':showdate, 'points':points}import numpy as npimport pandas as pdcnbo2021top5=pd.DataFrame(filmdescript,index=films)cnbo2021top5[['prices','persons']]=cnbo2021top5[['prices','persons']].astype(int)cnbo2021top5['bos']=cnbo2021top5['bos'].str.replace(',','').astype(int)cnbo2021top5['showdate']=cnbo2021top5['showdate'].astype('datetime64')cnbo2021top5['points']=cnbo2021top5['points'].apply(lambda x:float(x) if x!= '' else 0)

About cnboo1.xlsx, I put it in my code cloud, friends who need it download it themselves: cnboo1.xlsx

#read and sort datasets import pandas as pd cnbodf=pd.read_excel ('cnboo1.xlsx') cnbodfsort=cnbodf.sort_values(by=['BO'],ascending=False)

def mkpoints(x,y): #Write points rating return len(str(x))*(y/25)-3cnbodfsort['points']=cnbodfsort.apply(lambda x:mkpoints(x.BO,x.PERSONS),axis=1)

cnbodfsort.to_excel("cnbodfsort.xlsx",index=False) #Create an Excel file from matplotlib import pyplot as plt.rcParams <$'font.sans-serif']=<$'Microsoft YaHei']#Use Microsoft YaHei fonts plt.title("Box Office 2021TOP5") plt.xlabel("x-axis") plt.ylabel("y-axis")x=cnbo2021top5.persons.sort_values()y=cnbo2021top5.prices.sort_values()plt.plot(x,y,marker=". ",markersize=20,color='red',linewidth=4,markeredgecolor='blue')plt.show()

#Line chart progression from matplotlib import pyplot as plt plt.rcParams <$'font.sans-serif']=<$'Microsoft YaHei']#Use Microsoft Yahei font plt.title ("China Box Office 2021TOP5") plt.plot (bo,prices,label ='box office vs. ticket prices') plt.plot (bo,persons,label='box office vs. visits') plt.plot(bo,points,label=' box office vs. evaluations') plt.legend() #display label plt.xlabel ('box office ') #abscissa plt.ylabel ('quotes') #ordinate plt.show()

Change the layout.

#Line chart progression from matplotlib import pyplot as plt plt.rcParams <$'font.sans-serif']=<$'Microsoft YaHei']#Use Microsoft Yahei font plt.title ("China Box Office 2021TOP5") plt.plot (bo,prices,'r^--', label=' box office vs. ticket prices') plt.plot (bo,persons,'g*-', label=' box office vs. persons') plt.plot (bo,points,color ='blue ',marker ='o',markersize=10,label ='box office & evaluation') plt.legend() #display label plt.xlabel ('box office')#abscissa title plt.ylabel ('quotation ') #ordinate title plt.show()

V. Fill the broken line chart

Fill line chart: When determining a point above a data line, you can fill the upper and lower parts of the point with different colors.

dev_x=[25,26,27,28,29,30] #Developer's age dev_y=[7567,8789,8900,11560,16789,25231] #Income py_dev_y=[5567,6789,9098,15560,20789,23231] # Python Developer js_dev_y=[6567,7789,8098,12356, 14789,20231] # java Developer devsalary=pd.DataFrame ([dev_x,dev_y,py_dev_y,js_dev_y])devsalaryT=pd.DataFrame (devsalary.values.T,columns=["Age","Dev","Python","Java"])#Draw shaded line charts from matplotlib import pyplot as plt plt.style. use ('classic')plt.figure (figsize=(7,4))plt.rcParams <$'font.sans-serif']=<$'Microsoft YaHei']#Use Microsoft Elegant Black font plt.title ("Developer Salary") baseline=10000plt.plot (devsalaryT["Age"],devsalaryT["Dev"],label="Total Salary")plt.plot (devsalaryT["Age"],devsalaryT["Python"],label="Python salary") #plt.fill_between that does not display the data label of legend if there is no label (devsalaryT["Age"],devsalaryT["Python"],baseline,where=(devsalaryT["Python"]>baseline),interpolate=True,color='yellow')plt.fill_between (devsalaryT["Age"],devsalaryT["Python"],baseline,where=(devsalaryT["Python"]baseline),interpolate=True,color='yellow',alpha=0.3)plt.fill_between (devsalaryT["Age"],devsalaryT["Python"],baseline,where=(devsalaryT["Python"]baseline),interpolate=True,color='pink',alpha=0.7,label="more than 10000 yuan")plt.fill_between (devsalaryT["Age"],devsalaryT["Python"],baseline,where=(devsalaryT["Python"]baseline),interpolate=True,color='green',alpha=0.7, label="Above Population")plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],devsalaryT["Dev"],where=(devsalaryT["Python"]

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report