python数据可视化-matplotlib(一)

1. 绘制简单的折线图

import matplotlib.pyplot as plt
squares = [1, 4, 9, 26, 27]
fig, ax = plt.subplots()
ax.plot(squares)

plt.show()

 2. 修改标签文字和线条粗细

import matplotlib.pyplot as plt
import matplotlib

# 把语言设置为黑体
matplotlib.rc("font", family='Hei')

squares = [1, 4, 9, 26, 27]
fig, ax = plt.subplots()
ax.plot(squares, linewidth=3)

# 设置图标标题并给坐标轴加上标签
ax.set_title('平方数', fontsize=24)
ax.set_xlabel('值', fontsize=14)
ax.set_ylabel('值的平方', fontsize=14)

# ax.plot(squares)
ax.tick_params(axis='both' , labelsize=14)
plt.show()

 运行结果:

3. 校正图形

向polt()提供一系列数时,它假设第一个数据点对应的x坐标为0,但这里第一个点对应的x值为1,为了改变默认行为,可向plot()同时提供输入值和输出值。

import matplotlib.pyplot as plt
import matplotlib

# 把语言设置为黑体
matplotlib.rc("font", family='Hei')

imput_value = [1, 2, 3, 4, 5]
squares = [1, 4, 9, 16, 25]
fig, ax = plt.subplots()
ax.plot(imput_value, squares, linewidth=3)

# 设置图标标题并给坐标轴加上标签
ax.set_title('平方数', fontsize=24)
ax.set_xlabel('值', fontsize=14)
ax.set_ylabel('值的平方', fontsize=14)

# ax.plot(squares)
ax.tick_params(axis='both' , labelsize=14)
plt.show()

 运行结果:

4. 使用内置样式

matplotlib提供了很多已经定义好的样式

查看有哪些样式,在console中输入

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']

添加‘dark_background’样式后

import matplotlib.pyplot as plt
import matplotlib

# 把语言设置为黑体
matplotlib.rc("font", family='Hei')

imput_value = [1, 2, 3, 4, 5]
squares = [1, 4, 9, 16, 25]

plt.style.use('dark_background')
fig, ax = plt.subplots()
ax.plot(imput_value, squares, linewidth=3)

# 设置图标标题并给坐标轴加上标签
ax.set_title('平方数', fontsize=24)
ax.set_xlabel('值', fontsize=14)
ax.set_ylabel('值的平方', fontsize=14)

# ax.plot(squares)
ax.tick_params(axis='both' , labelsize=14)
plt.show()

 运行结果:

 5. 使用scatter()绘制散点图并设置样式

  • 绘制单个点,可使用scatter(),传递一堆x坐标和y坐标,她将在制定位置绘制一个点
    import matplotlib.pyplot as plt
    import matplotlib
    
    # 把语言设置为黑体
    matplotlib.rc("font", family='Hei')
    
    imput_value = [1, 2, 3, 4, 5]
    squares = [1, 4, 9, 16, 25]
    
    plt.style.use('fast')
    fig, ax = plt.subplots()
    ax.scatter(2, 4)
    # ax.plot(imput_value, squares, linewidth=3)
    
    # 设置图标标题并给坐标轴加上标签
    ax.set_title('平方数', fontsize=24)
    ax.set_xlabel('值', fontsize=14)
    ax.set_ylabel('值的平方', fontsize=14)
    
    # ax.plot(squares)
    ax.tick_params(axis='both' , labelsize=14)
    plt.show()

    运行结果:

6. 设置绘制图形时使用的点的尺寸,ax.scatter(2, 4, s=2000)

import matplotlib.pyplot as plt
import matplotlib

# 把语言设置为黑体
matplotlib.rc("font", family='Hei')

imput_value = [1, 2, 3, 4, 5]
squares = [1, 4, 9, 16, 25]

plt.style.use('fast')
fig, ax = plt.subplots()
ax.scatter(2, 4, s=2000)
# ax.plot(imput_value, squares, linewidth=3)

# 设置图标标题并给坐标轴加上标签
ax.set_title('平方数', fontsize=24)
ax.set_xlabel('值', fontsize=14)
ax.set_ylabel('值的平方', fontsize=14)

# ax.plot(squares)
# 设置刻度标记的大小
ax.tick_params(axis='both', which='major', labelsize=14)
# ax.tick_params(axis='both',  labelsize=14)
plt.show()

 运行结果:

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值