简体   繁体   English

如何在 Python 中自定义刻度线和柱线?

[英]How to custom ticks and bars in Python?

I want to see the frequency of occurrence of some event, when my event happened and when didn't.我想查看某个事件发生的频率,我的事件何时发生,何时未发生。

How can I custom the plot in a way that have years on the x-axis from 1970 to 2020 in order (1years interval), and have the bars where there are data and empty ticks where there are no data.我如何以一种方式自定义绘图,在 x 轴上按顺序从 1970 年到 2020 年(1 年间隔)显示年份,并在有数据的地方使用条形图,在没有数据的地方使用空刻度。

list1=[2019, 2016, 2014, 2014, 2011, 2008, 2005, 2004, 2003, 1994, 1992, 1989, 1988, 1984, 1983, 1981, 1980, 1979, 1977, 1975, 1973, 1970]
a=pd.Series(data=list1);a

A=a.value_counts(sort=False).plot.bar(figsize=(15, 3), color='darkblue')

plt.title(' A Occurrence ')

Thank you.谢谢你。

try this:尝试这个:

import pandas as pd
import matplotlib.pyplot as plt

list1=[2019, 2016, 2014, 2014, 2011, 2008, 2005, 2004, 2003, 1994, 1992, 1989, 1988, 1984, 1983, 1981, 1980, 1979, 1977, 1975, 1973, 1970]
a = pd.Series(list1)
A = a.value_counts().to_frame()
A.columns = ['cnt']
idx = np.arange(min(list1), max(list1)+1)
A = A.reindex(index=idx.tolist(), fill_value=0)
A.sort_index(axis=0,inplace=True)
A.plot.bar(figsize=(15, 3), color='darkblue')

plt.title(' A Occurrence ')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM