本站消息

  出租广告位,需要合作请联系站长

  今日名言-想象你自己对困难作出的反应,不是逃避或绕开它们,而是面对它们,同它们打交道,以一种进取的和明智的方式同它们奋斗 。——马克斯威尔·马尔兹

  今日名言-用谅解、宽恕的目光和心理看人、待人。人就会觉得葱笼的世界里,春意盎然,到处充满温暖。——蔡文甫


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

一行Python代码有多强,可让图形秒变「手绘风」

发布于2021-07-25 06:38     阅读(422)     评论(0)     点赞(6)     收藏(4)


之前介绍过一个绘制手绘风格图形的工具cutecharts:一款蠢萌蠢萌的可视化工具

但是,其功能有限,今天再介绍一个手绘工具(matplotlib.pyplot.xkcd()),一行代码可将所有Matplotlib和Seaborn绘制的图形变为手绘风格

matplotlib.pyplot.xkcd()简介

这个Matplotlib子函数特别简单,只有三个参数,别看参数少,但功能可不小

  1. matplotlib.pyplot.xkcd(scale=1, #相对于不使用xkcd的风格图,褶皱的幅度
  2. length=100, #褶皱长度
  3. randomness=2#褶皱的随机性
  4. )

matplotlib.pyplot.xkcd()使用

如下,加with行代码即可,括号中参数按个人喜好决定是否设置~

  1. with plt.xkcd(scale=1, length=100, randomness=2):
  2. #with是临时使用一下,不影响其它图使用正常样式
  3. 绘图代码
  4. 。。。。。。
  5. plt.show()

matplotlib.pyplot.xkcd()使用实例 

下面代码为pythonic生物人公众号之前的文章代码

以下参考:Python可视化25|seaborn绘制矩阵图

  1. #支持seaborn
  2. import seaborn as sns
  3. iris_sns = sns.load_dataset("iris")
  4. with plt.xkcd():
  5. g = sns.pairplot(
  6. iris_sns,
  7. hue='species', #按照三种花分类
  8. palette=['#dc2624', '#2b4750', '#45a0a2'])
  9. sns.set(style='whitegrid')
  10. g.fig.set_size_inches(12, 12)
  11. sns.set(style='whitegrid', font_scale=1.5)

  

以下参考:Python可视化29|matplotlib-饼图(pie) 

  1. import matplotlib.pyplot as plt
  2. with plt.xkcd(
  3. scale=4, #相对于不使用xkcd的风格图,褶皱的幅度
  4. length=120, #褶皱长度
  5. randomness=2): #褶皱的随机性
  6. plt.figure(dpi=150)
  7. patches, texts, autotexts = plt.pie(
  8. x=[1, 2, 3], #返回三个对象
  9. labels=['A', 'B', 'C'],
  10. colors=['#dc2624', '#2b4750', '#45a0a2'],
  11. autopct='%.2f%%',
  12. explode=(0.1, 0, 0))
  13. texts[1].set_size('20') #修改B的大小
  14. #matplotlib.patches.Wedge
  15. patches[0].set_alpha(0.3) #A组分设置透明度
  16. patches[2].set_hatch('|') #C组分添加网格线
  17. patches[1].set_hatch('x')
  18. plt.legend(
  19. patches,
  20. ['A', 'B', 'C'], #添加图例
  21. title="Pie Learning",
  22. loc="center left",
  23. fontsize=15,
  24. bbox_to_anchor=(1, 0, 0.5, 1))
  25. plt.title('Lovely pie', size=20)
  26. plt.show()

  1. with plt.xkcd():
  2. from string import ascii_letters
  3. plt.figure(dpi=150)
  4. patches, texts, autotexts = plt.pie(
  5. x=range(1, 12),
  6. labels=list(ascii_letters[26:])[0:11],
  7. colors=[
  8. '#dc2624', '#2b4750', '#45a0a2', '#e87a59', '#7dcaa9', '#649E7D',
  9. '#dc8018', '#C89F91', '#6c6d6c', '#4f6268', '#c7cccf'
  10. ],
  11. autopct='%.2f%%',
  12. )
  13. plt.legend(
  14. patches,
  15. list(ascii_letters[26:])[0:11], #添加图例
  16. title="Pie Learning",
  17. loc="center left",
  18. bbox_to_anchor=(1, 0, 0.5, 1),
  19. ncol=2, #控制图例中按照两列显示,默认为一列显示,
  20. )

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. with plt.xkcd():
  4. plt.figure(dpi=150)
  5. labels = ['Jack', 'Rose', 'Jimmy']
  6. year_2019 = np.arange(1, 4)
  7. year_2020 = np.arange(1, 4) + 1
  8. bar_width = 0.4
  9. plt.bar(
  10. np.arange(len(labels)) - bar_width / 2, #为了两个柱子一样宽
  11. year_2019,
  12. color='#dc2624',
  13. width=bar_width,
  14. label='year_2019' #图例
  15. )
  16. plt.bar(
  17. np.arange(len(labels)) + bar_width / 2,
  18. year_2020,
  19. color='#45a0a2',
  20. width=bar_width,
  21. label='year_2020' #图例
  22. )
  23. plt.xticks(np.arange(0, 3, step=1), labels, rotation=45) #定义柱子名称
  24. plt.legend(loc=2) #图例在左边

以下参考:Python可视化|matplotlib12-垂直|水平|堆积条形图详解 

以下参考: Python可视化|matplotlib10-绘制散点图scatter

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import pandas as pd
  4. from pandas import Series, DataFrame
  5. #数据准备
  6. from sklearn import datasets
  7. iris = datasets.load_iris()
  8. x, y = iris.data, iris.target
  9. pd_iris = pd.DataFrame(np.hstack((x, y.reshape(150, 1))),
  10. columns=[
  11. 'sepal length(cm)', 'sepal width(cm)',
  12. 'petal length(cm)', 'petal width(cm)', 'class'
  13. ])
  14. with plt.xkcd():
  15. plt.figure(dpi=150) #设置图的分辨率
  16. #plt.style.use('Solarize_Light2') #使用Solarize_Light2风格绘图
  17. iris_type = pd_iris['class'].unique() #根据class列将点分为三类
  18. iris_name = iris.target_names #获取每一类的名称
  19. colors = ['#dc2624', '#2b4750', '#45a0a2'] #三种不同颜色
  20. markers = ['$\clubsuit$', '.', '+'] #三种不同图形
  21. for i in range(len(iris_type)):
  22. plt.scatter(
  23. pd_iris.loc[pd_iris['class'] == iris_type[i],
  24. 'sepal length(cm)'], #传入数据x
  25. pd_iris.loc[pd_iris['class'] == iris_type[i],
  26. 'sepal width(cm)'], #传入数据y
  27. s=50, #散点图形(marker)的大小
  28. c=colors[i], #marker颜色
  29. marker=markers[i], #marker形状
  30. #marker=matplotlib.markers.MarkerStyle(marker = markers[i],fillstyle='full'),#设置marker的填充
  31. alpha=0.8, #marker透明度,范围为0-1
  32. facecolors='r', #marker的填充颜色,当上面c参数设置了颜色,优先c
  33. edgecolors='none', #marker的边缘线色
  34. linewidths=1, #marker边缘线宽度,edgecolors不设置时,该参数不起作用
  35. label=iris_name[i]) #后面图例的名称取自label
  36. plt.legend(loc='upper right')

 Ref:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.xkcd.html#matplotlib.pyplot.xkcd

原文链接:https://blog.csdn.net/qq_21478261/article/details/118963753



所属网站分类: 技术文章 > 博客

作者:空气很好

链接:http://www.pythonpdf.com/blog/article/445/556665fe126003466cbd/

来源:编程知识网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

6 0
收藏该文
已收藏

评论内容:(最多支持255个字符)