今天是生信星球陪你的第989天
公众号里的文章大多数需要编程基础,如果因为代码看不懂,而跟不上正文的节奏,可以来找我学习,相当于给自己一个新手保护期。我的课程都是循环开课。下一期的时间,点进去咨询微信↓
生信分析直播课程(10月初下一期)
生信新手保护学习小组(预计9.13下一期)
单细胞陪伴学习小组(预计9.16下一期)
1.单变量和双变量画图
(1)单变量图之直方图
import matplotlib.pyplot as plt
import seaborn as sns
iris = sns.load_dataset('iris')
list(iris)
#hist
plt.hist(iris['sepal_length'])
plt.show()
(2)双变量图之散点图
#scatter
plt.scatter(iris['sepal_length'], iris['sepal_width'])
plt.show()
练习
total_bill
列画直方图tip
列为横坐标,total_bill
列为纵坐标画散点图import matplotlib.pyplot as plt
import seaborn as sns
tips = sns.load_dataset('tips')
# Univariate histogram
plt____
____
# Bivariate scatterplot
____
plt.show()
import matplotlib.pyplot as plt
import seaborn as sns
tips = sns.load_dataset('tips')
# Univariate histogram
plt.hist(tips.total_bill)
plt.show()
# Bivariate scatterplot
plt.scatter(tips.tip,tips.total_bill)
plt.show()
(3)设置标题和横纵坐标
fig,ax = plt.subplots()
ax.scatter(iris['sepal_length'],iris['sepal_width'])
ax.set_title('Sepal Length')
ax.set_xlabel('Sepal Length')
ax.set_ylabel('Sepal Width')
plt.show()
fig,ax = plt.subplots()
ax.scatter(iris['sepal_length'],iris['sepal_width'])
ax.set_title('Sepal Length')
ax.set_xlabel('Sepal Length')
ax.set_ylabel('Sepal Width')
plt.xticks(rotation = 45)
plt.show()
练习
用.set_title()
设置标题为Histogram
,用.set_xlabel
设置横轴标题为Total Bill
import matplotlib.pyplot as plt
import seaborn as sns
tips = sns.load_dataset('tips')
# Create a figure with 1 axes
fig, ax = plt.subplots()
# Draw a histplot
ax = sns.histplot(tips['total_bill'], kde = True)
# Label the title and x axis
ax.set_title('Histogram')
ax.set_xlabel('Total Bill')
plt.show()
2.子图
插播:figure和axes以及axis的关系
什么是 figure?
什么是 axes?
(1)figure里只有一张子图
fig,ax = plt.subplots()
ax.scatter(iris['sepal_length'],iris['sepal_width'])
plt.show()
(2)figure里有多张子图
fig,(ax1,ax2) = plt.subplots(1, 2)
ax1.scatter(iris['sepal_length'],iris['sepal_width'])
ax2.hist(iris['sepal_length'])
plt.show()
练习:子图
import matplotlib.pyplot as plt
import seaborn as sns
tips = sns.load_dataset('tips')
# Create a figure with 1 axes
fig, ax = plt.____(1, 1)
# Plot a scatter plot in the axes
____.scatter(tips____, tips____)
plt.show()
# Create a figure with scatter plot and histogram
fig, (ax1, ax2) = plt.subplots(____, ____)
____(tips['tip'], tips['total_bill'])
____(tips['total_bill'])
plt.show()
import matplotlib.pyplot as plt
import seaborn as sns
tips = sns.load_dataset('tips')
# Create a figure with 1 axes
fig, ax = plt.subplots()
# Plot a scatter plot in the axes
ax.scatter(tips.tip, tips.total_bill)
plt.show()
# Create a figure with scatter plot and histogram
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.scatter(tips['tip'], tips['total_bill'])
ax2.hist(tips['total_bill'])
plt.show()
练习:axes用于seaborn
# histplot of tip
import seaborn as sns
tips = sns.load_dataset('tips')
dis = sns.histplot(tips['tip'], kde = True)
# Print the type
print(type(dis))
## <class 'matplotlib.axes._subplots.AxesSubplot'>
# Figure with 2 axes: regplot and histplot
fig, (ax1, ax2) = plt.subplots(1,2)
sns.histplot(tips['tip'], ax=ax1, kde = True)
sns.regplot(x='total_bill', y='tip', data=tips, ax=ax2)
plt.show()