In operation and maintenance management, often encounter time series data, such as network card traffic, number of online users, number of concurrent connections, and so on. Scatter plots are used to visually visualize the distribution of data.
The Pyplot of the Matplotlib module has a function of drawing a scatter plot, but the function requires that the x-axis be a numeric type. In the Pandas plot function, the scatter chart type ' Scatter ' also requires a digital type, with the time type of error. After reading dozens of articles in the search, the paper explores the simple way to draw a scatter plot. Scatter plots can be drawn using Pyplot's Plot_date ().
Here's the full Python code:
#-*-Coding:utf-8-*-"" "Speed1219.csv data file Format:dtime,speed 2017-12-19 10: 33:30,803 2017-12-19 10:35:29,1008 2017-12-19 10:36:04,1016 2017-12-19 10:37:32,984 2017-12-19 10:38:06,1008 "" "Import pandas as PD import Matplotlib.pyplot as plt from Matplotlib.dates I Mport Autodatelocator, Dateformatter df = pd.read_csv (' d:/speed1219.csv ', parse_dates=[' dtime ']) plt.plot _date (Df.dtime, df.speed, fmt= ' B. ') ax = PLT.GCA () ax.xaxis.set_major_formatter (Dateformatter ('%y-%m-%d%h:% M ')) #设置时间显示格式 Ax.xaxis.set_major_locator (Autodatelocator (maxticks=24)) #设置时间间隔 plt.xticks (rotation =90, ha= ' center ') label = [' Speedpoint '] plt.legend (label, loc= ' upper right ') Plt.grid () ax.se T_title (U ' transfer rate ', fontproperties= ' Simhei ', fontsize=14) Ax.set_xlabel (' Dtime ') Ax.set_ylabel (' Speed (kb/s) ') Plt.show ()
If not, you can delete the line starting with ax. Keep only the following lines of code:
df = pd.read_csv(‘d:/speed1219.csv‘, parse_dates=[‘dtime‘]) plt.plot_date(df.dtime, df.speed, fmt=‘b.‘) plt.xticks(rotation=90, ha=‘center‘) plt.grid() plt.show()
Python Draw time Series scatter plot