【发布时间】:2022-01-17 21:12:46
【问题描述】:
下面的代码使用graph_vals 和Monthly_indx 输出条形图,但是图表Monthly_indx 的x 值被弄乱了,在显示时无法读取。我如何才能修复 x 轴并使图形可读?
代码:
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
graph_vals = np.array([0.0, 15.58698486544634, 62.39522773472678, 53.102459267328186,
20.664598839134662, 37.43748926661284, 24.919985065059905, 2.462993387475203,
20.53746782047295, 87.85805111410333, 38.70428059326558, 31.56608531221292,
7.418190883067737, 17.10281023326888, 31.804189226978277, 30.05396124982128,
64.40080197706149, 33.29684656571501, 52.913157810513006, 20.913775029581142,
68.41187918506716, 47.56850690090707, 56.2736117598243, 31.25351961092795,
72.49099807107945, 44.95116145453175, 78.0696975321658, 54.60288966093484])
Monthly_indx = pd.date_range( pd.to_datetime('2019-05-08'), pd.to_datetime('2021-09-08'), freq='MS')
print(len(graph_vals), len(Monthly_indx))
ax1 = pd.DataFrame({'Monthly PnLs':graph_vals, 'Months':Monthly_indx })
ax = ax1.plot.bar(y='Monthly PnLs',x='Months', rot=0)
plt.setp(ax.get_xticklabels(), rotation=30, horizontalalignment='right')
输出:
【问题讨论】:
标签: python arrays pandas numpy date