【python 作日期的折线图和柱状图组合图】

在这里插入代码片
``###法一:但是没有标数据
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif'] = ['SimHei']  # 用黑体显示中文
import pandas as pd
import matplotlib.pyplot as plt
example = {'Timestamp':
          ['2021-10', '2021-11', '2021-12', '2022-01', '2022-02', '2022-03',
           '2022-04', '2022-05', '2022-06', '2022-07', '2022-08','2022-09','2022-10'],            
           '数':
           [7,178, 437, 280, 204,249,844,475,468,264,490,822,561]}

df = pd.DataFrame(example)```
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
#设置格式
sns.set_style("whitegrid")
 

x = df['Timestamp']
y =df['数']
#设置图形大小
plt.rcParams['figure.figsize'] = (12.0,5.0)
fig = plt.figure()
#画柱形图
ax1 = fig.add_subplot(111)
ax1.bar(x,y,alpha=.7,color='g')

ax1.set_ylabel('数',fontsize='15')
#ax1.set_title("数据统计",fontsize='20')
#画折线图 
ax2 = ax1.twinx()   #组合图
ax2.set_ylabel('',fontsize='15')
ax2.plot(x, y, 'r',ms=10)

plt.show()
###法二
有标数据

import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif'] = ['SimHei']  # 用黑体显示中文
import pandas as pd
import matplotlib.pyplot as plt
example = {'Timestamp':
          ['2021-10', '2021-11', '2021-12', '2022-01', '2022-02', '2022-03',
           '2022-04', '2022-05', '2022-06', '2022-07', '2022-08','2022-09','2022-10'],            
           '数':
           [7,178, 437, 280, 204,249,844,475,468,264,490,822,561]}

df = pd.DataFrame(example)

x = df['Timestamp']
y =df['弹幕数'] 
plt.bar(x=x, height=y, label='', color='steelblue', alpha=0.8)

for x1, yy in zip(x, y):
    plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=10, rotation=0)
plt.title("弹幕数随日期活跃度")
plt.xlabel("发布日期")
plt.ylabel("数量")
plt.legend()
plt.plot(x, y, "r", marker='*', ms=10, label="a")
plt.xticks(rotation=45)
plt.legend(loc="upper left")
plt.savefig("a.jpg")
plt.show()



你可能感兴趣的:(python,matplotlib,开发语言)