Line chart for Python data visualization

Line chart for Python data visualization

提示:前言
Line chart for Python data visualization


提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档


foreword

In this tutorial, you'll learn enough Python to create professional-looking line charts. Then, in the exercises below, you'll apply your new skills to real-world datasets


提示:以下是本篇文章正文内容,下面案例可供参考

1. Import package

import pandas as pd
pd.plotting.register_matplotlib_converters()
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
print("Setup Complete")

2. Select the data set

The dataset for this tutorial tracks global daily streams on the music streaming service Spotify. We focus on five top songs of 2017 and 2018:

Note that the first date to appear is January 6, 2017, which corresponds to the release date of Ed Sheeran's "The Shape of You". And, using the table, you can see that "Shape of You" was played 12,287,078 times around the world on the day it was released. Note that the other songs have missing values ​​in the first row because they were released later!
insert image description here

# Path of the file to read
spotify_filepath = "../input/spotify.csv"

# Read the file into a variable spotify_data
spotify_data = pd.read_csv(spotify_filepath, index_col="Date", parse_dates=True)

insert image description here
We can print the first five rows of the dataset using the head command you learned about in the previous tutorial

# Print the first 5 rows of the data
spotify_data.head()

insert image description here
Now check that the first five rows match the dataset image above (from where we see how it looks in Excel). Empty entries will be shown as NaN, which is short for "Not a Number"

We can also see the last five rows of the data by making just one small change, where .head() becomes .tail() :

# Print the last five rows of the data
spotify_data.tail()

insert image description here
Thankfully, everything looks good, and with millions of global streams per song per day, we can move on to charting the data!

3. Line chart

Now that the dataset is loaded into the notebook, we only need one line of code to make the line chart

# Line chart showing daily global streams of each song 
sns.lineplot(data=spotify_data)

insert image description here

As you can see above, this line of code is relatively short and has two main components:

sns.lineplot tells the notebook that we want to create a line chart.
Every command you learn in this course will start with sns, which indicates that the command is from the seaborn package. For example, we use sns.lineplot to make a line graph. You'll see shortly that we use sns.barplot and sns.heatmap to make bar and heatmaps, respectively.
data=spotify_data selects the data that will be used to create the chart.

Note that you will always use the same format when creating a line chart, and with new datasets, the only thing that changes is the name of the dataset. So, for example, if you were working with a different dataset called financial_data, the line of code would look like this:

sns.lineplot(data=financial_data)

4. The size of the graph and the title of the chart

Sometimes we want to modify other details, such as the size of the graph and the title of the chart. Each of these options can be easily set with a single line of code.

# Set the width and height of the figure
plt.figure(figsize=(14,6))

# Add title
plt.title("Daily Global Streams of Popular Songs in 2017-2018")

# Line chart showing daily global streams of each song 
sns.lineplot(data=spotify_data)

insert image description here
The first line of code sets the size of the graphic to 14 inches (width) by 6 inches (height). To set the size of any figure, you just copy the same line of code where it appears. Then, if you want to use a custom size, change the provided values ​​of 14 and 6 to the desired width and height.

The second line of code sets the title of the graph. Note that titles must always be enclosed in quotation marks ("…")!

5. Plot a subset of the data

So far, you have learned how to draw a line for each column in the dataset. In this section, you will learn how to plot subsets of columns.

We'll start by printing the names of all the columns. This is done in one line of code, just swap out the name of the dataset (in this case spotify_data) and it will work for any dataset.

list(spotify_data.columns)

insert image description here

In the next code cell, we draw the lines corresponding to the first two columns in the dataset.

# Set the width and height of the figure
plt.figure(figsize=(14,6))

# Add title
plt.title("Daily Global Streams of Popular Songs in 2017-2018")

# Line chart showing daily global streams of 'Shape of You'
sns.lineplot(data=spotify_data['Shape of You'], label="Shape of You")

# Line chart showing daily global streams of 'Despacito'
sns.lineplot(data=spotify_data['Despacito'], label="Despacito")

# Add label for horizontal axis
plt.xlabel("Date")

insert image description here
The first two lines of code set the title and size of the graph (should look familiar!).

The next two lines each add a line to the line chart. For example, consider the first one, which adds the line "your shape":

# Line chart showing daily global streams of 'Shape of You'
sns.lineplot(data=spotify_data['Shape of You'], label="Shape of You")

This line looks very similar to the code we use to plot each row in the dataset, but it has a few key differences:

Instead of setting data=spotify_data, we set data=spotify_data['Shape of You'] . Usually, to plot only a single column, we use this format, enclosing the column name in single quotes and enclosing it in square brackets. (To make sure you specified the column names correctly, you can use the command you learned above to print a list of all column names.) We
also add label="Shape of You" to make the lines appear in the legend and set their corresponding labels.
The last line of code modifies the labels of the horizontal axis (or x-axis), where the desired labels are enclosed in quotation marks ("...").

6. Change the style

With just one line of code, we can quickly change the style of a graph to a different theme.

# Change the style of the figure to the "dark" theme
sns.set_style("dark")

# Line chart 
plt.figure(figsize=(12,6))
sns.lineplot(data=spotify_data)

insert image description here

Seaborn has five different themes:
(1) "darkgrid",
(2) "whitegrid",
(3) "dark",
(4) "white",
(5) "ticks",
you just need to use commands like to the one in the code cell above (filled with the selected theme) to change it.

Reference link
https://www.kaggle.com/code/alexisbcook/line-charts

Guess you like

Origin blog.csdn.net/weixin_39559994/article/details/128813688