更改pyplot图形的x值的方向

2024-05-26 18:09:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下数据:

stage_summary = {'Day1': {'1': 100.0, '2': 0.0, '3': 0.0, '4': 0.0, '5': 0.0, '6': 0.0, '7': 0.0, '8': 0.0}, 'Day2': {'1': 100.0, '2': 0.0, '3': 0.0, '4': 0.0, '5': 0.0, '6': 0.0, '7': 0.0, '8': 0.0}, 'Day3': {'1': 89.8, '2': 10.2, '3': 0.0, '4': 0.0, '5': 0.0, '6': 0.0, '7': 0.0, '8': 0.0}, 'Day4': {'1': 58.6, '2': 41.4, '3': 0.0, '4': 0.0, '5': 0.0, '6': 0.0, '7': 0.0, '8': 0.0}, 'Day5': {'1': 52.71, '2': 47.06, '3': 0.0, '4': 0.0, '5': 0.0, '6': 0.23, '7': 0.0, '8': 0.0}, 'Day6': {'1': 47.89, '2': 50.65, '3': 0.0, '4': 0.0, '5': 0.0, '6': 1.46, '7': 0.0, '8': 0.0}, 'Day7': {'1': 49.72, '2': 46.95, '3': 0.0, '4': 0.0, '5': 0.0, '6': 3.33, '7': 0.0, '8': 0.0}, 'Day8': {'1': 52.59, '2': 39.35, '3': 0.0, '4': 0.0, '5': 0.0, '6': 8.05, '7': 0.0, '8': 0.01}, 'Day9': {'1': 55.45, '2': 30.73, '3': 0.0, '4': 0.0, '5': 0.0, '6': 13.74, '7': 0.0, '8': 0.08}} 

date_list = ['2019-05-28',
 '2019-05-29',
 '2019-05-30',
 '2019-05-31',
 '2019-06-03',
 '2019-06-04',
 '2019-06-05',
 '2019-06-06',
 '2019-06-07']

我以这种方式绘制数据,如线形图:

for each_state in list(range(1,9)):
    tmp = []
    for each_day in stage_summary:
        tmp.append(stage_summary[each_day][str(each_state)])
    plt.plot(date_list, tmp, label=str('State ' + str(each_state)))
plt.ylabel('Probability')
plt.xlabel('Days')
plt.legend(loc='best')  

这将导致下图,
enter image description here

但是,正如您所看到的,x标签是日期,是长字符串。因此,它必须旋转并垂直显示以使其可读。但我不确定如何在绘图仪中更改x值的方向。我怎样才能做到


Tags: 数据infordatepltsummarystagetmp
2条回答

设置plotly.graph_objs.Layout.xaxis.tickangle=90

详情:https://plot.ly/python/axes/#set-and-style-axes-title-labels-and-ticks

您使用的是matplotlib而不是plotly,对吗

如果是,请尝试:

for each_state in list(range(1,9)):
    tmp = []
    for each_day in stage_summary:
        tmp.append(stage_summary[each_day][str(each_state)])
    plt.plot(date_list, tmp, label=str('State ' + str(each_state)))
plt.ylabel('Probability')
plt.xlabel('Days')
plt.xticks(rotation=45)
plt.legend(loc='best')  

相关问题 更多 >

    热门问题