今天开始在使用eng版来学习 line two 的课程
先来做个复习
而且计划直接使用3rd来学习,let’s go
现在发现英文读起来很舒服,而且不用英文来学习,用中文生硬的来背诵,是比较无效的学习
import matplotlib.pyplot as plt
squares = [1, 4, 9, 16, 25]
fig, ax = plt.subplots()
ax.plot(squares)
plt.show()
We create a list called squares to hold the data that we’ll plot. Then we follow another common Matplotlib convention by calling the subplots() function .
This function can generate one or more plots in the same figure.
The variable fig represents the entire figure or collection of plots(代表整个) that
are generated.
The variable ax represents a single plot in the figure and is the variable we’ll use most of the time.
Using Built-in Styles
To see the full list of available styles, run the following lines in a terminal session:
Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
>>> plt.style.available
['Solarize_Light2', '_classic_test_patch', '_mpl-gallery', '_mpl-gallery-nogrid',
'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight',
'ggplot', 'grayscale', 'seaborn-v0_8', 'seaborn-v0_8-bright',
'seaborn-v0_8-colorblind', 'seaborn-v0_8-dark', 'seaborn-v0_8-dark-palette',
'seaborn-v0_8-darkgrid', 'seaborn-v0_8-deep', 'seaborn-v0_8-muted',
'seaborn-v0_8-notebook', 'seaborn-v0_8-paper', 'seaborn-v0_8-pastel',
'seaborn-v0_8-poster', 'seaborn-v0_8-talk', 'seaborn-v0_8-ticks',
'seaborn-v0_8-white', 'seaborn-v0_8-whitegrid', 'tableau-colorblind10']
Plotting and Styling Individual Points with scatter()
use scatter() to plot a single point
import matplotlib.pyplot as pl
pl.style.use('seaborn-v0_8')
fig, ax = pl.subplots()
ax.scatter(2,4)
pl.show()
style the output to make it more interesting
add a title, label, the axes, make sure all the text is large and enough to read
import matplotlib.pyplot as pl
pl.style.use('seaborn-v0_8')
fig, ax = pl.subplots()
ax.scatter(2,4,s=200)
# set chart title and label axes.
ax.set_title("Square Number", fontsize=24)
ax.set_xlabel("Value",fontsize=14)
ax.set_ylabel("Square of Value", fontsize=14)
#set size of tick labels
ax.tick_params(labelsize = 14)
pl.show()
Plotting a Series of Points with scatter()
we can pass scatter() separate lists of x- and
y-values, like this
import matplotlib.pyplot as pl
x_values = [1,2,3,4,5]
y_values = [1,4,9,16,25]
pl.style.use('seaborn-v0_8')
fig, ax = pl.subplots()
ax.scatter(x_values,y_values,s=100)
# set chart title and label axes.
ax.set_title("Square Number", fontsize=24)
ax.set_xlabel("Value",fontsize=14)
ax.set_ylabel("Square of Value", fontsize=14)
#set size of tick labels
ax.tick_params(labelsize = 14)
pl.show()
Calculating Data Automatically
import matplotlib.pyplot as plt
x_values = range(1,1001)
y_values = [x**2 for x in x_values]
plt.style.use('seaborn-v0_8')
fig, ax =plt.subplots()
ax.scatter(x_values,y_values,s=10)
# set chart title and label axes.
ax.set_title("Square Number", fontsize=24)
ax.set_xlabel("Value",fontsize=14)
ax.set_ylabel("Square of Value", fontsize=14)
#Set the range for each axis.
ax.axis([0,1100,0,1_100_000])
plt.show()
a list generates the y-values by looping through the x-values (for x in x_value)
Before showing the plot, we use the axis() method to specify the
range of each axis .
The axis() method requires four values: the minimum and maximum values for the x-axis and the y-axis.
Here, we run the x-axis from 0 to 1,100 and the y-axis from 0 to 1,100,000.
这里调整如果调整 ax.axis的值变成 ([0,1000,0,1_000_000])
就会是这样的效果
知道了,原来这个ax.axis是坐标轴的最大最小值,要比Value 大一些,这样会更舒服
Customizing Tick Labels
Matplotlib defaults to scientific notation for tick labels
The ticklabel_format() method allows you to override the default tick
label style for any plot
--snip--
#Set the range for each axis.
ax.axis([0,1100,0,1_100_000])
ax.ticklabel_format(style='plain')
plt.show()
Defining Custom Colors
plt.style.use('seaborn-v0_8')
fig, ax =plt.subplots()
ax.scatter(x_values,y_values,color='red',s=10)
we can also define custom colors use the RGB color model
pass the color argument a tuple (元祖)with three float values (one each for red, green, and blue, in that order)
ax.scatter(x_values,y_values,color=(0,0.8,0),s=10)