Matplotlib 实践总结

导入matplotlib包

使用%matplotlib inline可以使得matplotlib在jupyter中显示图片

%matplotlib inline
import matplotlib.pyplot as plt

创建一个简单的图像画面

# Plot X & y (the lists you've created)
plt.plot(x,y)

另外一种面向对象的创建figure的方法如下

# Create a plot using plt.subplots()
fig,ax=plt.subplots()

接下来展示一个small  matplotlib workflow.

# Import and get matplotlib ready
%matplotlib inline
import matplotlib.pyplot as plt

# Prepare data (create two lists of 5 numbers, X & y)
X=[1,2,3,4,5]
y=[6,7,8,9,55]

# Setup figure and axes using plt.subplots()
fig,ax=plt.subplots()



# Add data (X, y) to axes
ax.plot(X,y)

# Customize plot by adding a title, xlabel and ylabel
ax.set(title="Sample simple plot",
       xlabel="x-axis",
       ylabel="y-axis")

# Save the plot to file using fig.savefig()
fig.savefig("../images/simple-plot.png")

需要使用到画图模块函数直接使用ax.plot(kind="模块")或者ax.plot.模块()

使用字典创建数据类型,然后可视化

favourite_food_prices={"Almond butter": 10,
                         "Blueberries": 5,
                         "Eggs": 6}
fig,ax=plt.subplots()
ax.bar(favourite_food_prices.keys(), favourite_food_prices.values())

# Add a title, xlabel and ylabel to the plot
ax.set(title="Daniel's favourite foods",
       xlabel="Food",
       ylabel="Price($)")

接下来制作多画面图形,示例代码如下

# Create the same plot as above with 2 rows and 2 columns and figsize of (10, 5)
fig,((ax1,ax2),(ax3,ax4))=plt.subplots(nrows=2,
                                       ncols=2,
                                       figsize=(10,5))

# Plot X versus X/2 on the top left axes
ax1.plot(X,X/2)

# Plot a scatter plot of 10 random numbers on each axis on the top right subplot
ax2.scatter(np.random.random(10),np.random.random(10))

# Plot a bar graph of the favourite food keys and values on the bottom left subplot
ax3.bar(favourite_food_prices.keys(),favourite_food_prices.values())

# Plot a histogram of 1000 random normally distributed numbers on the bottom right subplot
ax4.hist(np.random.randn(1000))

在dataframe中的某一个series应用正则处理示例代码

import re
car_sales["Price"]=car_sales["Price"].apply(lambda x:re.sub("[\$\,\.]", "",x))

单个图片的完整绘图流,附加定制图片

# Replicate the above plot in whichever way you see fit

# Note: The method below is only one way of doing it, yours might be
# slightly different

# Create DataFrame with patients over 50 years old
over_50=heart_disease[heart_disease["age"]>50]

# Create the plot
fig,ax=plt.subplots(figsize=(10,6))

# Plot the data
scatter=ax.scatter(over_50["age"],
                   over_50["chol"],
                   c=over_50["target"])

# Customize the plot
ax.set(title="Heart Disease and Cholesterol Levels",
       xlabel="Age",
       ylabel="Cholesterol")
ax.legend(*scatter.legend_elements(),title="Target")

# Add a meanline
ax.axhline(over_50["chol"].mean(),
        linestyle="--")

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值