Python-Matplotlib绘制简单图像

1.plot 绘制直线图

import matplotlib.pyplot as plt
import matplotlib

x = [1,2,3]
y = [4,5,6]

plt.plot(a,b)
plt.show()

在这里插入图片描述
每次都执行plt.show()才显示图像,比较麻烦,我们运行一下代码%matplotlib inline这样以后用plt.plot()就可以显示图像了,下面代码承接上文代码:

plt.plot(x,y,'r--',label="aaa")
plt.legend() # 配合plt.plot(label="aaa")用于显示图例

matplotlib.rcParams['font.family']='SimHei'
matplotlib.rcParams['font.size'] = 12

plt.xlabel("x 轴")
plt.ylabel("y 轴")

在这里插入图片描述

  • plt.plot()

    • 第一参数为x轴的值,第二个参数为y轴的值
    • 第三个参数指明图像线的属性(本例中是用红色的虚线)
    • 第四个参数指明图像中的线是指什么,即图例(plt.legend()用于将图例显示在图像上)
  • plt.xlabel(“x 轴”) 设置x,y轴名称

2. subplot 子图

plt.subplot(211)plt.subplot(2,1,1)
其实就是对图做了切分,现在展示的是一个2行1列的,最后的参数指明是哪一个图plt.subplot(2,1,1)就是第一个,plt.subplot(2,1,2)就是第二个

1.绘制折线图,并给出解决中文无法显示的问题

import numpy as np
import matplotlib.pyplot as plt 
import matplotlib

#方法一,改变全局字体

matplotlib.rcParams['font.family']='SimHei'  # 'fangsong'
matplotlib.rcParams['font.size'] = 12
matplotlib.rcParams['axes.unicode_minus']=False #用来正常显示负号

plt.subplot(211)
plt.plot([3,1,4,5,2],label = '报考人数') #添加label时要用legend()函数
plt.legend(loc = "best")
plt.xlabel("横轴")
plt.ylabel("纵轴(值)")

#方法二,plt.xlabel()内增加fontproperties,fontsize

plt.subplot(212)
x = np.arange(0.0,5.0,0.02)
y = np.cos(2*np.pi*x)
plt.xlabel('横轴,时间',fontproperties='simhei',fontsize = 20)
plt.ylabel('纵轴,时间',fontproperties='simhei',fontsize= 10)
plt.plot(x,y,'r--')
plt.show()

这里写图片描述

绘制直方图

import matplotlib.pyplot as plt 
import numpy as np

# 生成需要显示的数据
np.random.seed(0)
mu,sigma = 100,20
a = np.random.normal(mu,sigma,size = 100)

#第二个参数为直方图的个数,histtype = 'stepfilled'设置柱子之间颜色间隙
plt.hist(a, bins=10, normed=0, histtype = 'stepfilled',facecolor="blue", edgecolor="black", alpha=0.7)
# plt.hist(a,10,normed = 1,histtype = 'stepfilled',facecolor = 'b',alpha = 0.75)
plt.title('Histgram')
plt.show()

plt.hist()中参数rwidth=0.9可调节柱之间的间距
新版本不用normed ,改用density.

plt.hist(a, bins=10, density = 1, facecolor="blue", edgecolor="black", alpha=0.7)

改进前
在这里插入图片描述

改进后(2019-7-22)
在这里插入图片描述

绘制散点图

https://www.cnblogs.com/sunshinewang/p/6853813.html

import numpy as np
import matplotlib.pyplot as plt
N = 1000
x = np.array([0,1,2,3,4,5,6,7,8,9])
y = np.array([1,1,1,-1,-1,-1,1,1,1,-1])
plt.scatter(x, y)
plt.show()

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt

N = 1000
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = np.array([5.56, 5.70, 5.91, 6.4, 6.8, 7.05, 8.9, 8.7, 9, 9.05])
plt.scatter(x, y)
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = np.array([5.72, 5.72, 5.72, 6.46, 6.46, 6.46, 9.13, 9.13, 9.13, 9.13])
plt.scatter(x, y, c='red')
plt.show()

在这里插入图片描述

解决中文无法显示&&负号无法显示的问题

方法一,改变全局字体

import matplotlib
matplotlib.rcParams['font.family']='SimHei' # 'fangsong'
matplotlib.rcParams['font.size'] = 12
matplotlib.rcParams['axes.unicode_minus']=False #用来正常显示负号

方法二,plot()内增加fontproperties,fontsize

plt.xlabel('横轴,时间',fontproperties='simhei',fontsize = 20)
plt.ylabel('纵轴,时间',fontproperties='simhei',fontsize= 10)

绘制3D图像

https://blog.csdn.net/shu15121856/article/details/72590620#

热力图

import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

sns.set(style='whitegrid', color_codes=True)

data = np.array([[0,0,0,1],[0,0,0,1],[0,0,0,0],[0,0,0,0]])
sns.heatmap(data)

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值