使用python画图表_termgraph.py 一个python命令行工具,用于在终端中绘制基本图表...

Termgraph

A python command-line tool which draws basic graphs in the terminal.

Graph types supported:

Bar Graphs

Color charts

Multi-variable

Stacked charts

Horizontal or Vertical

Emoji!

Examples

termgraph data/ex1.dat

# Reading data from data/ex1.dat

2007: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 183.32

2008: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 231.23

2009: ▇ 16.43

2010: ▇▇▇▇ 50.21

2011: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 508.97

2012: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 212.05

2014: ▏ 1.00

An example using emoji as custom tick:

termgraph data/ex1.dat --custom-tick "🏃" --width 20 --title "Running Data"

# Running Data

2007: 🏃🏃🏃🏃🏃🏃🏃 183.32

2008: 🏃🏃🏃🏃🏃🏃🏃🏃🏃 231.23

2009: 16.43

2010: 🏃 50.21

2011: 🏃🏃🏃🏃🏃🏃🏃🏃🏃🏃🏃🏃🏃🏃🏃🏃🏃🏃🏃🏃 508.97

2012: 🏃🏃🏃🏃🏃🏃🏃🏃 212.05

2014: 1.00

An example using stdin and emoji:

echo "Label,3,9,1" | termgraph --custom-tick "😀" --no-label

😀😀😀 3.00

😀😀😀😀😀😀😀😀😀 9.00

😀 1.00

Most results can be copied and pasted wherever you like, since they use standard block characters. However the color charts will not show, since they use terminal escape codes for color. A couple images to show color examples:

termgraph data/ex4.dat --color {blue,red}

loading.png

termgraph data/ex7.dat --color {yellow,magenta} --stacked --title "Stacked Data"

loading.png

Calendar Heatmap, expects first column to be date in yyyy-mm-dd

termgraph --calendar --start-dt 2017-07-01 data/cal.dat

loading.png

Install

Works best with Python3, you can install from PyPI project

pip3 install termgraph

Note: Be sure your PATH includes the pypi install directory, for me it is ~/.local/bin/

Usage

Create data file with two columns either comma or space separated. The first column is your labels, the second column is a numeric data

termgraph [datafile]

Help: termgraph -h

usage: termgraph [-h] [--title TITLE] [--width WIDTH] [--format FORMAT]

[--suffix SUFFIX] [--no-labels]

[--color [{red,blue,green,magenta,yellow,black,cyan} [{red,blue,green,magenta,yellow,black,cyan} ...]]]

[--vertical] [--stacked] [--different-scale] [--calendar]

[--start-dt START_DT] [--custom-tick CUSTOM_TICK]

[--delim DELIM] [--verbose]

[filename]

draw basic graphs on terminal

positional arguments:

filename data file name (comma or space separated). Defaults to

stdin.

optional arguments:

-h, --help show this help message and exit

--title TITLE Title of graph

--width WIDTH width of graph in characters default:50

--format FORMAT format specifier to use.

--suffix SUFFIX string to add as a suffix to all data points.

--no-labels Do not print the label column

--color [{red,blue,green,magenta,yellow,black,cyan} [{red,blue,green,magenta,yellow,black,cyan} ...]]

Graph bar color( s )

--vertical Vertical graph

--stacked Stacked bar graph

--different-scale Categories have different scales.

--calendar Calendar Heatmap chart

--start-dt START_DT Start date for Calendar chart

--custom-tick CUSTOM_TICK

Custom tick mark, emoji approved

--delim DELIM Custom delimiter, default , or space

--verbose Verbose output, helpful for debugging

Background

I wanted a quick way to visualize data stored in a simple text file. I initially created some scripts in R that generated graphs but this was a two step process of creating the graph and then opening the generated graph.

After seeing command-line sparklines I figured I could do the same thing using block characters for bar charts.

Contribute

For feature requests or bug reports, use Github Issues.

Thanks to all the additional Contributors.

License

MIT License, see LICENSE.txt

好的,我来帮你写这个程序。首先,你需要安装pandas和matplotlib这两个库,你可以在命令行使用以下命令进行安装: ``` pip install pandas matplotlib ``` 然后,你可以使用以下代码来读取Excel文件的数据,并计算出每个年龄段的占比: ```python import pandas as pd # 读取Excel文件的数据 df = pd.read_excel('data.xlsx') # 计算每个年龄段的数量 age_counts = df['age'].value_counts() # 计算每个年龄段的占比 age_percentages = age_counts / age_counts.sum() # 将占比转化为百分比形式 age_percentages = age_percentages.apply(lambda x: format(x, '.2%')) print(age_percentages) ``` 接下来,你可以使用以下代码来绘制柱形图: ```python import matplotlib.pyplot as plt # 设置图表样式 plt.style.use('ggplot') # 绘制柱形图 age_counts.plot(kind='bar', color='steelblue') # 添加x轴和y轴标签 plt.xlabel('Age') plt.ylabel('Count') # 显示图表 plt.show() ``` 最后,你可以使用以下代码将程序保存为rw2_1.py,并将图表保存为t2.png: ```python import matplotlib.pyplot as plt # 读取Excel文件的数据 df = pd.read_excel('data.xlsx') # 计算每个年龄段的数量 age_counts = df['age'].value_counts() # 计算每个年龄段的占比 age_percentages = age_counts / age_counts.sum() # 将占比转化为百分比形式 age_percentages = age_percentages.apply(lambda x: format(x, '.2%')) # 绘制柱形图 age_counts.plot(kind='bar', color='steelblue') # 添加x轴和y轴标签 plt.xlabel('Age') plt.ylabel('Count') # 保存图表 plt.savefig('t2.png') # 显示图表 plt.show() ``` 希望能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值