赞
踩
cnames = { 'aliceblue': '#F0F8FF', 'antiquewhite': '#FAEBD7', 'aqua': '#00FFFF', 'aquamarine': '#7FFFD4', 'azure': '#F0FFFF', 'beige': '#F5F5DC', 'bisque': '#FFE4C4', 'black': '#000000', 'blanchedalmond': '#FFEBCD', 'blue': '#0000FF', 'blueviolet': '#8A2BE2', 'brown': '#A52A2A', 'burlywood': '#DEB887', 'cadetblue': '#5F9EA0', 'chartreuse': '#7FFF00', 'chocolate': '#D2691E', 'coral': '#FF7F50', 'cornflowerblue': '#6495ED', 'cornsilk': '#FFF8DC', 'crimson': '#DC143C', 'cyan': '#00FFFF', 'darkblue': '#00008B', 'darkcyan': '#008B8B', 'darkgoldenrod': '#B8860B', 'darkgray': '#A9A9A9', 'darkgreen': '#006400', 'darkkhaki': '#BDB76B', 'darkmagenta': '#8B008B', 'darkolivegreen': '#556B2F', 'darkorange': '#FF8C00', 'darkorchid': '#9932CC', 'darkred': '#8B0000', 'darksalmon': '#E9967A', 'darkseagreen': '#8FBC8F', 'darkslateblue': '#483D8B', 'darkslategray': '#2F4F4F', 'darkturquoise': '#00CED1', 'darkviolet': '#9400D3', 'deeppink': '#FF1493', 'deepskyblue': '#00BFFF', 'dimgray': '#696969', 'dodgerblue': '#1E90FF', 'firebrick': '#B22222', 'floralwhite': '#FFFAF0', 'forestgreen': '#228B22', 'fuchsia': '#FF00FF', 'gainsboro': '#DCDCDC', 'ghostwhite': '#F8F8FF', 'gold': '#FFD700', 'goldenrod': '#DAA520', 'gray': '#808080', 'green': '#008000', 'greenyellow': '#ADFF2F', 'honeydew': '#F0FFF0', 'hotpink': '#FF69B4', 'indianred': '#CD5C5C', 'indigo': '#4B0082', 'ivory': '#FFFFF0', 'khaki': '#F0E68C', 'lavender': '#E6E6FA', 'lavenderblush': '#FFF0F5', 'lawngreen': '#7CFC00', 'lemonchiffon': '#FFFACD', 'lightblue': '#ADD8E6', 'lightcoral': '#F08080', 'lightcyan': '#E0FFFF', 'lightgoldenrodyellow': '#FAFAD2', 'lightgreen': '#90EE90', 'lightgray': '#D3D3D3', 'lightpink': '#FFB6C1', 'lightsalmon': '#FFA07A', 'lightseagreen': '#20B2AA', 'lightskyblue': '#87CEFA', 'lightslategray': '#778899', 'lightsteelblue': '#B0C4DE', 'lightyellow': '#FFFFE0', 'lime': '#00FF00', 'limegreen': '#32CD32', 'linen': '#FAF0E6', 'magenta': '#FF00FF', 'maroon': '#800000', 'mediumaquamarine': '#66CDAA', 'mediumblue': '#0000CD', 'mediumorchid': '#BA55D3', 'mediumpurple': '#9370DB', 'mediumseagreen': '#3CB371', 'mediumslateblue': '#7B68EE', 'mediumspringgreen': '#00FA9A', 'mediumturquoise': '#48D1CC', 'mediumvioletred': '#C71585', 'midnightblue': '#191970', 'mintcream': '#F5FFFA', 'mistyrose': '#FFE4E1', 'moccasin': '#FFE4B5', 'navajowhite': '#FFDEAD', 'navy': '#000080', 'oldlace': '#FDF5E6', 'olive': '#808000', 'olivedrab': '#6B8E23', 'orange': '#FFA500', 'orangered': '#FF4500', 'orchid': '#DA70D6', 'palegoldenrod': '#EEE8AA', 'palegreen': '#98FB98', 'paleturquoise': '#AFEEEE', 'palevioletred': '#DB7093', 'papayawhip': '#FFEFD5', 'peachpuff': '#FFDAB9', 'peru': '#CD853F', 'pink': '#FFC0CB', 'plum': '#DDA0DD', 'powderblue': '#B0E0E6', 'purple': '#800080', 'red': '#FF0000', 'rosybrown': '#BC8F8F', 'royalblue': '#4169E1', 'saddlebrown': '#8B4513', 'salmon': '#FA8072', 'sandybrown': '#FAA460', 'seagreen': '#2E8B57', 'seashell': '#FFF5EE', 'sienna': '#A0522D', 'silver': '#C0C0C0', 'skyblue': '#87CEEB', 'slateblue': '#6A5ACD', 'slategray': '#708090', 'snow': '#FFFAFA', 'springgreen': '#00FF7F', 'steelblue': '#4682B4', 'tan': '#D2B48C', 'teal': '#008080', 'thistle': '#D8BFD8', 'tomato': '#FF6347', 'turquoise': '#40E0D0', 'violet': '#EE82EE', 'wheat': '#F5DEB3', 'white': '#FFFFFF', 'whitesmoke': '#F5F5F5', 'yellow': '#FFFF00', 'yellowgreen': '#9ACD32'}
import numpy as np
import matplotlib.pylab as pl
%matplotlib inline
a = np.arange(0, 2.0 * np.pi, 0.1)
print(a)
b = np.cos(a)
pl.scatter(a, b)
# pl.show()
[0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. 1.1 1.2 1.3 1.4 1.5 1.6 1.7
1.8 1.9 2. 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3. 3.1 3.2 3.3 3.4 3.5
3.6 3.7 3.8 3.9 4. 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5. 5.1 5.2 5.3
5.4 5.5 5.6 5.7 5.8 5.9 6. 6.1 6.2]
<matplotlib.collections.PathCollection at 0x15465e2ff98>
import numpy as np
import matplotlib.pylab as pl
%matplotlib inline
x = np.random.random(100) # 0-1
print(x[0:10])
y = np.random.random(100)
pl.scatter(x, y, s=x * 500, c=u'r', marker=u'*')
pl.show()
# s 指定大小, c指定颜色, marker指定符号形状,
[0.15815532 0.65428075 0.80638782 0.17977135 0.86633134 0.60840757
0.95604924 0.92410809 0.48354069 0.47104306]
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# x = np.linspace(0,10,100)
x = np.arange(0, 6, 0.1) # 以0.1为单位,生成0到6的数据
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y1, label="sin")
plt.plot(x, y2, linestyle = "--", label="cos") # 用虚线绘制
plt.xlabel("x") # x轴标签
plt.ylabel("y") # y轴标签
plt.title('sin & cos') # 标题
plt.legend()
# plt.show()
<matplotlib.legend.Legend at 0x1545addeb38>
import matplotlib.pyplot as plt import numpy as np %matplotlib inline labels = ("Frogs", "Hogs", "Dogs", "Logs") colors = ["yellowgreen", "gold", "#FF0000", "lightcoral"] explode = (0, 0.1, 0, 0.1) # 使饼状图中第二片和第四片分开 fig = plt.figure() ax = fig.gca() ax.pie(np.random.random(4), # 四个值 explode = explode, labels = labels, colors = colors, autopct = "%1.1f%%", # 百分比的格式 小数点保留一位, %% = "%" shadow = True, # 是否显示阴影 startangle = 90, # 起始角度, 第一块从90度开始绘制, 从北向西转 radius = 0.25, # 饼的半径 center = (0, 0), # 中心位置 frame = True) # ax.set_xticks([0,1]) #设置坐标轴刻度 ax.set_yticks([0,1]) #设置坐标轴刻度 # 0, 1被替换 ax.set_xticklabels(["Sunny", "Cloudy"]) #设置坐标轴显示的标签 ax.set_yticklabels(["Dry", "Rainy"]) #设置坐标轴显示的标签 ax.set_xlim((-0.5,1.5)) # 设置坐标轴跨度 ax.set_ylim((-0.5,1.5)) # 设置坐标轴跨度 ax.set_aspect('equal') # 设置横纵比相等 plt.show()
import matplotlib.pyplot as plt import numpy as np %matplotlib inline x = np.linspace(0, 2*np.pi, 500) y = np.sin(x) z = np.cos(x*x) plt.figure(figsize=(8,4)) plt.plot(x,y, label="$sin(x)$", color="red", # 红色, 两个像素宽度 linewidth=2) plt.plot(x,z,"b--", # 蓝色虚线 label="$cos(x^2)$") plt.xlabel("time(s)") plt.ylabel("volt") plt.title("sin and cos figure using pyplot") plt.ylim(-1.2, 1.2) plt.legend() # 显示图例 左下角线的标识 plt.show() # 显示窗口
import matplotlib.pyplot as plt import numpy as np %matplotlib inline x = np.linspace(0, 2*np.pi, 500) y1 = np.sin(x) y2 = np.cos(x) y3 = np.cos(x*x) plt.figure(1)# 创建第一个图形 ax1 = plt.subplot(2,2,1) #第一行第一列第一个图形 plt.sca(ax1) # 选择ax1 plt.plot(x,y1,color="red") plt.ylim = (-1.2, 1.2) # 限制y轴范围 ax2 = plt.subplot(2,2,2) #第一行第2列第2个图形 plt.sca(ax2) # 选择ax2 plt.plot(x,y2,"b--") plt.ylim = (-1.2, 1.2) # 限制y轴范围 ax3 = plt.subplot(212, facecolor='r') # 背景色red plt.sca(ax3) # 选择ax3 plt.plot(x,y3,"g--") plt.ylim = (-1.2, 1.2) # 限制y轴范围 plt.legend() # 显示图例 左下角线的标识 plt.show() # 显示窗口
No handles with labels found to put in legend.
import matplotlib.pyplot as plt import numpy as np import mpl_toolkits.mplot3d x,y = np.mgrid[-2:2:20j, -2:2:20j] #步长使用j, 从-2到2分为了20个点 # 得到了20*20 400个点, 后面的j标识包含终点2 # print(x) z = 50*np.sin(x+y) ax = plt.subplot(111, projection='3d') ax.plot_surface(x, y, z, # 画一个面 rstride=2, cstride=1, cmap=plt.cm.Blues_r) ax.set_xlabel("X") ax.set_ylabel("Y") ax.set_zlabel("Z") plt.legend() # 显示图例 左下角线的标识 plt.show() # 显示窗口 rho,theta = np.mgrid[0:1:40j, 0:2*np.pi:40j] z = rho**2 x = rho*np.cos(theta) y = rho*np.sin(theta) ax = plt.subplot(111, projection="3d") ax.plot_surface(x,y,z) plt.show()
No handles with labels found to put in legend.
import matplotlib.pyplot as plt import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import numpy as np # import mpl_toolkits.mplot3d mpl.rcParams["legend.fontsize"] = 10 # 图例字号 fig = plt.figure() ax = fig.gca(projection="3d") theta = np.linspace(-4 * np.pi, 4 * np.pi, 100) z = np.linspace(-4, 4, 100) * 0.3 r = z**3 + 1 x = r * np.sin(theta) y = r * np.cos(theta) ax.plot(x, y, z, label="parametric curve") ax.legend() plt.show()
第一个图
# 绘制三个平台的准确率 # 读取不同的csv数据 import numpy as np import pandas as pd import json #绘制多个条形图 from matplotlib import pyplot as plt from pylab import * # 中文显示问题 mpl.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False #绘制多个条形图 from matplotlib import pyplot as plt %matplotlib inline # jupyter专用 # 输入统计数据 display_index = ["a","b","c","d","e","f"] platform = ['x轴1','x轴2','x轴3'] colors = ["red",'b','purple','mediumblue','g','navy','red'] data = {"a": [1.071, 2.559, 2.292], "b": [1.218, 2.682, 2.381], "c": [1.104, 2.755, 2.381], "d": [1.217, 2.762, 2.325], "e": [1.27, 2.904, 2.307], "f": [1.254, 3.4, 2.376]} # with open("data.json", "r") as f: # data = json.loads(f.read()) print(data) # print(display_index) #定义变量 bar_width = 0.1 # 条形宽度 # 定义横坐标 共三组 a = np.arange(len(platform)) # 使用7次 bar 函数画出三组组条形图 for i,display_ind in enumerate(display_index): plt.bar(a+bar_width*i, # 每增加一个bar, 就以a向右移动width宽 height=data[display_ind], width=bar_width-0.02, # -0.02的目的是为了让显示出来空隙 color=colors[i], # 颜色, 这里需要看颜色对照表 label=display_ind) # 标签 # 显示图例 bbox_to_anchor plt.legend(loc='center left', bbox_to_anchor=(0, 1.12), # 表示上下左右 ncol与(左,下)的距离 ncol=3) # 每行显示3个 # 设置坐标系x轴 plt.xticks(a + bar_width*3, # a+bar_width*3是为了显示刻度中间 platform, # platform x轴显示的数据 size=12, # size是用来设置显示字体大小 rotation=30) # 设置X轴的下标旋转30度 plt.xlabel('平台',size = 12) # plt.ylabel("y轴显示", size = 12) # 纵坐标轴标题 # plt.title('xxxxxxxx', size=20) # 图形标题 # plt.savefig("保存结果.pdf",dpi=150,bbox_inches="tight") # 保存为pdf格式 plt.savefig("保存结果.png",dpi=150,bbox_inches="tight") # 保存为图片格式 plt.show()
参数 | 说明 | 类型 |
---|---|---|
x | x坐标 | int;float |
height | 条形的高度 | int, float |
width | 宽度 | 0~1,默认0.8 |
botton | 条形的起始位置 | 也是y轴的起始坐标 |
align | 条形的中心位置 | “center”,"lege"边缘 |
color | 条形的颜色 | “r",“b”,“g”,"#123465",默认“b" |
edgecolor | 边框的颜色 | 同上 |
linewidth | 边框的宽度 | 像素,默认无,int |
tick_label | 下标的标签 | 可以是元组类型的字符组合 |
log | y轴使用科学计算法表示 | bool |
orientation | 是竖直条还是水平条 | 竖直:“vertical”,水平条:“horizontal” |
hatch | 填充形状 | str格式 如"+","-" |
解决的问题:
第二个图, 增加bar不同形状填充
# 绘制三个平台的准确率 # 读取不同的csv数据 import numpy as np import pandas as pd import json #绘制多个条形图 from matplotlib import pyplot as plt from pylab import * # 中文显示问题 mpl.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False #绘制多个条形图 from matplotlib import pyplot as plt %matplotlib inline # jupyter专用 # 输入统计数据 display_index = ["a","b","c","d","e","f"] platform = ['x轴1','x轴2','x轴3'] colors = ["red",'b','purple','mediumblue','g','navy','red'] hatchs = ['///', '\\\\\\', '||||', '--', '+++', '***', '...'] data = {"a": [1.071, 2.559, 2.292], "b": [1.218, 2.682, 2.381], "c": [1.104, 2.755, 2.381], "d": [1.217, 2.762, 2.325], "e": [1.27, 2.904, 2.307], "f": [1.254, 3.4, 2.376]} # with open("data.json", "r") as f: # data = json.loads(f.read()) print(data) # print(display_index) #定义变量 bar_width = 0.1 # 条形宽度 # 定义横坐标 共三组 a = np.arange(len(platform)) # 使用7次 bar 函数画出三组组条形图 for i,display_ind in enumerate(display_index): plt.bar(a+bar_width*i, # 每增加一个bar, 就以a向右移动width宽 height=data[display_ind], width=bar_width-0.02, # -0.02的目的是为了让显示出来空隙 color=colors[i], # 颜色, 这里需要看颜色对照表 hatch = hatchs[i], label=display_ind) # 标签 # 显示图例 bbox_to_anchor plt.legend(loc='center left', bbox_to_anchor=(0, 1.12), # 表示上下左右 ncol与(左,下)的距离 ncol=3) # 每行显示3个 # 设置坐标系x轴 plt.xticks(a + bar_width*3, # a+bar_width*3是为了显示刻度中间 platform, # platform x轴显示的数据 size=12, # size是用来设置显示字体大小 rotation=30) # 设置X轴的下标旋转30度 plt.xlabel('平台',size = 12) # plt.ylabel("y轴显示", size = 12) # 纵坐标轴标题 # plt.title('xxxxxxxx', size=20) # 图形标题 # plt.savefig("保存结果.pdf",dpi=150,bbox_inches="tight") # 保存为pdf格式 plt.savefig("保存结果.png",dpi=150,bbox_inches="tight") # 保存为图片格式 plt.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。