Plotting with pandas

The matplotlib drawing package in python is already very powerful, but if we use pandas to analyze the data, the data format is DataFrame, we need to use pandas to draw graphics. Here I mainly talk about how the function DataFrame.plot draws.

Plotting with pandas is very simple, as shown in Figure 1 below, I randomly generated a 5*4 2D array and made them into a line graph. It's like Figure 2. Pandas directly distinguishes the four columns of ABCD when drawing, and treats them as different y values, and the index value of df is regarded as the x value. Then four lines were drawn.
write picture description here
write picture description here

Let's look at a slightly more complicated one. The following example reads data from a json file. The fields of the json data are these:
user_id user ID
lab lab name
course course name
minutes learning minutes
created_at learning time

We only need to use user_id as the x-axis, minutes as the y-axis, and draw a line graph of the total learning time. So read data from json and convert it to DataFrame data format.

import matplotlib.pyplot as plt
import pandas as pd

def data_plot(file):
    fig = plt.figure()
    ax = fig.add_subplot(111)

    with open(file,'r') as fp:
        data = fp.read()

    df = pd.read_json(data)
    user_df = df[['user_id','minutes']].groupby('user_id').sum()
    ax=user_df.plot(ax=ax)

    ax.set_title("Study Data")
    ax.set_xlabel("User ID")
    ax.set_ylabel("Study Time"
    plt.show()

if __name__ == '__main__':
    data_plot('user_study.json')

Since I want to add things like a title, I need to know the parameters of the plot function. The parameters are as follows:

DataFrame.plot(x=None, y=None, kind='line', ax=None, 
subplots=False, sharex=None, sharey=False, 
layout=None,figsize=None, use_index=True, title=None, 
grid=None, legend=True, style=None, logx=False, 
logy=False,loglog=False, xticks=None, yticks=None, xlim=None,
 ylim=None, rot=None, fontsize=None, colormap=None,table=False, 
**kwds)

A detailed explanation of these parameters can be found in this article: https://blog.csdn.net/sinat_24395003/article/details/60364345

According to the request, the picture I want to come out is like this. So I set the title of the picture and the labels of the x and y axes according to the parameters of the plot function. At first I wrote this:

user_df.plot(title="Study Data",x="User ID",y="Study Time")
plt.show()

But it reported an error, KeyError. I didn't find the reason, maybe there is something wrong with my understanding of the two parameters of x and y. So I used the ax parameter. ax can be understood as a subfigure on a figure object. By setting ax, I can also achieve the effect I want.
write picture description here

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326883123&siteId=291194637