• matplotlib(4)-- 数据覆盖坐标轴时的相关处理、散点图scatter、条形图bar


     1 import matplotlib.pyplot as plt
     2 import numpy as np
     3 
     4 x = np.linspace(-3, 3, 50)
     5 y1 = 2 * x + 1
     6 
     7 #figure 1
     8 plt.figure()
     9 plt.plot(x, y1, linewidth = 10,zorder=1)
    10 
    11 
    12 #横纵坐标轴显示范围设置
    13 plt.xlim((-2, 2))
    14 plt.ylim((-10, 10))
    15 
    16 #坐标轴的移动 gca = “get current axis”
    17 ax = plt.gca()
    18 ax.spines["right"].set_color("none")
    19 ax.spines["top"].set_color("none")
    20 ax.xaxis.set_ticks_position("bottom")
    21 ax.yaxis.set_ticks_position("left")
    22 ax.spines["bottom"].set_position(("data", 0))   #Set the X and Y coordinates of the sprite simultaneously
    23 ax.spines["left"].set_position(("data", 0))
    24 
    25 #当出现数据覆盖坐标值的情况:
    26 
    27 #方法一:直接将plt.plot(x, y1, linewidth = 10)修改为plt.plot(x, y1, linewidth = 10,zorder=1)即可
    28 #方法二:将每一个坐标值取出来,作相关处理,使其覆盖在数据上,以达到可视化处理
    29 for label in ax.get_xticklabels() + ax.get_yticklabels():
    30     label.set_fontsize(10)
    31     label.set_zorder(1)
    32     label.set_bbox(dict(facecolor = "White", edgecolor = "None", alpha = 0.1))
    33 
    34 plt.show()
     1 import matplotlib.pyplot as plt
     2 import numpy as np
     3 
     4 n = 1024
     5 X = np.random.normal(0, 1, n)
     6 Y = np.random.normal(0, 1, n)
     7 T = np.arctan2(Y, X)
     8 
     9 #关于scatter的相关参数介绍,参考博文 https://blog.csdn.net/qiu931110/article/details/68130199
    10 plt.scatter(X, Y, s = 75, c = T, alpha = 0.5)
    11 
    12 plt.xlim((-1.5, +1.5))
    13 plt.ylim((-1.5, +1.5))
    14 
    15 plt.show()
     1 import matplotlib.pyplot as plt
     2 import numpy as np
     3 
     4 n = 1024
     5 X = np.random.normal(0, 1, n)
     6 Y = np.random.normal(0, 1, n)
     7 T = np.arctan2(Y, X)
     8 
     9 #关于scatter的相关参数介绍,参考博文 https://blog.csdn.net/qiu931110/article/details/68130199
    10 plt.scatter(X, Y, s = 75, c = T, alpha = 0.5)
    11 plt.xlim((-1.5, +1.5))
    12 plt.ylim((-1.5, +1.5))
    13 
    14 #将坐标标度隐藏
    15 plt.xticks(())
    16 plt.yticks(())
    17 
    18 plt.show()
     1 import matplotlib.pyplot as plt
     2 import numpy as np
     3 
     4 n = 12
     5 X = np.arange(n)
     6 Y1= (1 - X/float(n)) * np.random.uniform(0.5, 1.0, n)
     7 Y2= (1 - X/float(n)) * np.random.uniform(0.5, 1.0, n)
     8 
     9 #plt.bar()参数解释,参考博文 https://www.cnblogs.com/shine-rainbow/p/10742952.html
    10 #绘制条形图
    11 plt.bar(X, +Y1, facecolor = "#9999ff", edgecolor = "white")
    12 plt.bar(X, -Y2, facecolor = "#ff9999", edgecolor = "white")
    13 
    14 #text()参数解释
    15 #####################
    16 # plt.text(x, y, string, fontsize=15, verticalalignment="top", horizontalalignment="right")
    17 # 参数:
    18 # x,y:表示坐标值上的值
    19 # string:表示说明文字
    20 # fontsize:表示字体大小
    21 # verticalalignment:垂直对齐方式 ,参数:[ ‘center’ | ‘top’ | ‘bottom’ | ‘baseline’ ]
    22 # horizontalalignment:水平对齐方式 ,参数:[ ‘center’ | ‘right’ | ‘left’ ]
    23 
    24 for x,y in zip(X, Y1):
    25     plt.text(x, y, "%.2f"%y, ha = "center", va = "bottom")
    26 
    27 for x,y in zip(X, -Y2):
    28     plt.text(x, y, "%.2f"%y, ha = "center", va = "top")
    29 
    30 plt.xticks(())
    31 plt.yticks(())
    32 
    33 plt.show()
  • 相关阅读:
    django中的分页管理
    python 时间字符串和时间戳之间的转换
    随机生成英文字母,并且统计频数
    html 中a标签的问题(无反应,跳转,调用方法)
    oracle sid,instance_name,db_name,oracle_sid之间的关系
    备份
    修改yum源
    sql基本语法
    第二章 shell编程基础
    第一章 初识shell程序
  • 原文地址:https://www.cnblogs.com/guoruxin/p/11248242.html
Copyright © 2020-2023  润新知