002 Python数据可视化之柱状图、饼图、集成图

接续前文《001 Python数据可视化之折线图》,试想更进一步的场景:此厂CEO想了解这两种成品近一年每个季度的次品数及各季度次品数占比。

本文继续用前文中模拟的数据,并以成品A为例进行演示,仍继续基于Matplotlib来进行数据展示。

#提高图片显示的分辨率,如果无此语句,图片显示不清晰(jupyter中)

%config InlineBackend.figure_format = 'svg'

#导入matplotlib的子库pyplot并命名为plt

import matplotlib.pyplot as plt

import numpy as np

#使图形中的中文正常编码显示

plt.rcParams['font.sans-serif'] = ['SimHei']

#使坐标轴刻度正常显示负号

plt.rcParams['axes.unicode_minus'] = False

#模拟生产数据

period = range(-12,0)

compA_defectNum = [10, 8, 9, 12, 9, 10, 9, 5, 4, 5, 3, 4]

compB_defectNum = [26, 28, 25, 24, 25, 22, 20, 8, 9, 10, 7, 8]

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个简单的例子,展示如何使用Python中的一些常用数据库画出雷达、柱、词云和,并创建数据大屏: 1. 雷达 ```python import matplotlib.pyplot as plt import numpy as np # 创建数据 categories = ['A', 'B', 'C', 'D', 'E'] values = [5, 3, 4, 2, 6] # 计算角度 angles = np.linspace(0, 2*np.pi, len(categories), endpoint=False) # 闭合形 values = np.concatenate((values, [values[0]])) angles = np.concatenate((angles, [angles[0]])) # 创建雷达 fig = plt.figure() ax = fig.add_subplot(111, polar=True) ax.plot(angles, values, 'o-', linewidth=2) ax.fill(angles, values, alpha=0.25) ax.set_thetagrids(angles * 180/np.pi, categories) ax.grid(True) # 显示像 plt.show() ``` 2. 柱 ```python import matplotlib.pyplot as plt # 创建数据 x = ['A', 'B', 'C', 'D', 'E'] y = [5, 3, 4, 2, 6] # 创建柱 fig, ax = plt.subplots() ax.bar(x, y) # 添加标签和标题 ax.set_xlabel('Categories') ax.set_ylabel('Values') ax.set_title('Example Bar Chart') # 显示像 plt.show() ``` 3. 词云 ```python from wordcloud import WordCloud import matplotlib.pyplot as plt # 创建词云 text = "Hello World! This is an example text for wordcloud." wordcloud = WordCloud().generate(text) # 显示词云 plt.imshow(wordcloud, interpolation='bilinear') plt.axis("off") plt.show() ``` 4. ```python import matplotlib.pyplot as plt # 创建数据 labels = ['A', 'B', 'C', 'D', 'E'] sizes = [5, 3, 4, 2, 6] # 创建 fig, ax = plt.subplots() ax.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90) # 添加标题 ax.set_title('Example Pie Chart') # 显示像 plt.show() ``` 5. 创建数据大屏 一般来说,创建数据大屏需要用到前端技术和可库,比如D3.js、Echarts等。这里提供一个简单的Python库dash,可以用Python代码创建交互式的数据大屏。 ```python import dash import dash_core_components as dcc import dash_html_components as html # 创建app app = dash.Dash() # 创建布局 app.layout = html.Div(children=[ html.H1(children='Hello Dash'), html.Div(children=''' Dash: A web application framework for Python. '''), dcc.Graph( id='example-graph', figure={ 'data': [ {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'Category 1'}, {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': 'Category 2'}, ], 'layout': { 'title': 'Example Dash Plot' } } ) ]) # 启动app if __name__ == '__main__': app.run_server(debug=True) ``` 上面的代码创建了一个简单的Dash应用,包括一个标题、一个段落和一个柱。你可以根据需要修改这个例子,添加更多的组件和布局,创建你需要的数据大屏。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值