Python Application: Data Visualization Part 3: Various Graphs

Line graph

Set the marker type and color

matplotlib.pyplot.plot(x, y, marker="Marker type", markerfacecolor="Marker color") 
#Data x on the horizontal axis, data y on the vertical axis
# marker="Specifier"If you specify, the marker type (shape) is set.
# markerfacecolor="Specifier"If you specify, the marker color is set.
Specifier type
"o" Circle
"s" square
"p" pentagon
"*" Star
"+" plus
"D" Diamond
Specifier color
"b" Blue
"g" Green
"r" Red
"c2 cyan
"m" Magenta
"y" yellow
"k" black
"w" White

Set line style and color

matplotlib.pyplot.plot(x, y, linestyle="Line style", color="Line color")

The treatment of colors is the same as the type of marker.

Specifier style
"-" Practice
"--" Dashed line
"-." Dashed line(Dot)
":" dotted line

bar graph

Creating a bar chart

matplotlib.pyplot.bar(x, y)

Set label on the horizontal axis

matplotlib.pyplot.bar(x, y, tick_label=[List of labels])

Create a stacked bar chart

A graph representing two or more series of data stacked for the same item This is called a stacked bar graph.

matplotlib.pyplot.bar(x, y, bottom=[List of data columns])
#If specified, the bottom margin can be set for the corresponding index.

#The series you want to display below when plotting the second and subsequent series
#You can create a stacked bar graph by specifying it as bottom.
plt.legend("Series 1 label", "Series 2 label") 
#If specified, the legend can be set.

histogram

Create a histogram

matplotlib.pyplot.hist(List type data array)

Counting the number of data that fits in each section

It is called the frequency distribution.

When visualizing the frequency distribution, it is called a histogram. Statistical graphs with frequencies (number of times) on the vertical axis and classes (range) on the horizontal axis are often used.

Set the number of bins

matplotlib.pyplot.hist(List type data column, bins=Number of bins)
#If you specify bins, you can divide into classes with any number of bins.
# bins="auto"If you specify, the number of bins will be set automatically.

When creating a histogram, it is important how many classes the data is divided into. The number of that class

It is called the number of bins.

Perform normalization

matplotlib.pyplot.hist(List type data column, density=True)
# density=If True is specified, the histogram can be normalized.

Assuming that the histogram distribution is normal Manipulating the histogram so that the sum is 1 is called normalization or standardization.

Create a cumulative histogram

matplotlib.pyplot.hist(List type data column, cumulative=True)
# cumulative=If you specify True, you can create a cumulative histogram.
Relative frequency
The frequency expressed as a percentage of the whole
Cumulative relative frequency
Sum of relative frequencies up to that class
The cumulative relative frequency will eventually be 1.
Cumulative histogram
Histogram representation of cumulative relative frequency
You can tell if it is fair by examining the increase or decrease in the cumulative histogram.

Scatter plot

Create a scatter plot

matplotlib.pyplot.scatter(x, y)
#Specify the data x on the horizontal axis and the corresponding data y on the vertical axis.

Set marker type and color

matplotlib.pyplot.scatter(x, y, marker="Marker type", color="Marker color")
#The types and colors are shown in a line graph.

Set the size of the marker according to the value

matplotlib.pyplot.scatter(x, y, s=Marker size)
#The default value is 20.

Set the marker density according to the value

matplotlib.pyplot.scatter(x, y, c=List-type data corresponding to marker color or plot data, cmap="Color system specifier")
# c=You can set the marker color by specifying the marker color

#Specify the list type data corresponding to the lot data in c
#Further cmap="Color system specifier"If you specify, you can display the marker in gradation with the darkness according to the value of c.
Color system specifier color
"Reds" Red
"Blues" Blue
"Greens" Green
"Purples" purple

Be careful of multiple systems.

Show color bars

matplotlib.pyplot.colorbar()
#When you display the color bar, you can see the approximate value by the darkness of the marker.

It corresponds to the right side of the figure below.

image.png

pie chart

Creating a pie chart

matplotlib.pyplot.pie() 
#Use this to draw a circle.
matplotlib.pyplot.axis("equal") 
#Here's how to make the graph circular, without this code it would be an ellipse.

Set a label on the pie chart

matplotlib.pyplot.pie(data, labels=[List of labels])

Make certain elements stand out

matplotlib.pyplot.pie(data, explode=[List of degree of prominence])
#Any element can be displayed separately.
#Specify a value from 0 to 1 for "Conspicuity" as list type data.

3D graph

Create 3D Axes

import matplotlib
matplotlib.pyplot.figure().add_subplot(1, 1, 1, projection="3d")

To draw a 3D graph, you need to create a subplot with 3D drawing capabilities Specify projection = "3d" when creating the subplot.

Create a curved surface

#Subplot is a variable`ax`in the case of
ax.plot_surface(X, Y, Z)

matplotlib.pyplot.show()#The drawn graph is output to the screen using this.

If you want to draw a graph that looks as close to true as possible Draw a curved surface by specifying the data corresponding to the x-axis, y-axis, and z-axis in plot_surface ().

Create a 3D histogram

#Subplot is a variable`ax`in the case of
ax.bar3d(xpos, ypos, zpos, dx, dy, dz)
# bar3d()Specify the data corresponding to the x-axis, y-axis, and z-axis positions and the amount of change in

Three-dimensional histograms and bar graphs are effective methods for finding the relationship between two elements. Each element of the dataset corresponds to the x-axis and y-axis, and is stacked in the z-axis direction.

Create a 3D scatter plot

x = np.ravel(X)
#Subplot is a variable`ax`in the case of
ax.scatter3D(x, y, z)

# scatter3D()Specify the data corresponding to the x-axis, y-axis, and z-axis in.

#Because the data you specify must be one-dimensional
#If it is not one-dimensional data, np in advance.ravel()Convert the data using.

A three-dimensional scatter plot has three types of data that are (or are likely to have) related to each other. Plotting in a three-dimensional space is useful for visually predicting trends in data.

Apply color map to 3D graph

#Import cm from matplotlib in advance.
import matplotlib.cm as cm

#Subplot is a variable`ax`in the case of
ax.plot_surface(X, Y, Z, cmap=cm.coolwarm)
#When plotting data, plot_surface()To cmap=cm.If you specify coolwarm
#You can apply a color map to the z-axis values.

3D graphs with monotonous colors may be difficult to see, such as areas with many irregularities. In that case, use the function to change the displayed color according to the coordinates taken by the points in the graph. You can make it easier to see.

Recommended Posts

Python Application: Data Visualization Part 3: Various Graphs
Python application: data visualization part 1: basic
Python application: data visualization # 2: matplotlib
Python Application: Data Handling Part 2: Parsing Various Data Formats
Python Application: Data Cleansing Part 1: Python Notation
Python Application: Data Handling Part 3: Data Format
Python Data Visualization Libraries
Various Python visualization tools
Python application: Pandas Part 1: Basic
Python application: Pandas Part 2: Series
Python application: Data handling Part 1: Data formatting and file input / output
Python application: Numpy Part 3: Double array
Easy data visualization with Python seaborn.
Data analysis starting with python (data visualization 1)
Data analysis starting with python (data visualization 2)
Python application: Data cleansing # 2: Data cleansing with DataFrame
[Python] Chapter 04-06 Various data structures (creating dictionaries)
Python visualization tool for data analysis work
Process Pubmed .xml data with python [Part 2]
[Python] Chapter 04-03 Various data structures (multidimensional list)
[Python] Chapter 04-04 Various data structures (see list)
Python application: Pandas Part 4: DataFrame concatenation / combination
[Python] Web application from 0! Hands-on (4) -Data molding-
[Python] Various data processing using Numpy arrays
[Python] Chapter 04-02 Various data structures (list manipulation)
[Python] Chapter 04-07 Various data structures (dictionary manipulation)
Recommendation of Altair! Data visualization with Python
QGIS + Python Part 2
Create test data like that with Python (Part 1)
Data acquisition from analytics API with Google API Client for python Part 2 Web application
QGIS + Python Part 1
Beautiful graph drawing with python -seaborn makes data analysis and visualization easier Part 1
Data analysis python
[Various image analysis with plotly] Dynamic visualization with plotly [python, image]
Real-time visualization of thermography AMG8833 data in Python
Beautiful graph drawing with python -seaborn makes data analysis and visualization easier Part 2
# 3 [python3] Various operators
Python: Scraping Part 1
Python3 Beginning Part 1
[python] Read data
Python: Scraping Part 2
Power BI visualization of Salesforce data entirely in Python
"My Graph Generation Application" by Python (PySide + PyQtGraph) Part 2
Web application made with Python3.4 + Django (Part.1 Environment construction)
[Python] Chapter 04-05 Various data structures (tuple creation and features)
"My Graph Generation Application" by Python (PySide + PyQtGraph) Part 1
Data analysis with python 2
Visualization memo by Python
Data analysis using Python 0
Data analysis overview python
Data cleaning using Python
Python basic memorandum part 2
Data visualization with pandas
Python basic memo --Part 2
Python data analysis template
[Python tutorial] Data structure
[Python] Sorting Numpy data
Python basic memo --Part 1
Application of Python 3 vars
Data analysis with Python
Logistics visualization with Python