matplotlib直方图轴格式化

时间:2017-04-27 09:56:17

标签: python matplotlib histogram

我有一个来自sql数据库查询的直方图。代码如下:

  @foreach (var file in Model.ExistingFiles)
                    {
                        <tr>
                            <td>
                                @{
                                    var length = @file.FileName.Length - 1;
                                }
                                @(@file.FileName.Length > 10 ? @file.FileName.Substring(0, 10) + "..." : @file.FileName)
                            </td>
                            <td>@file.FileSize</td>
                            <td>@file.UploadDate.ToString("MM/dd/yyyy")</td>
                            <td>
                                @Html.HiddenFor(x => x.Id)
                                @Html.HiddenFor(x => x.ClientId)
                                @Html.HiddenFor(x => x.FreelancerId)

                                <a class="posDelete" href="javascript:void(0)">
                                    <span id="fileSpan" class="noti glyphicon glyphicon-remove-circle" style="color:#D3B06E" onclick="DeleteFile('@file.Id','@file.FreelancerId','@file.Filecategory','@file.FileName');"></span>
                                </a>
                            </td>
                        </tr>
                                    }

输出如下: https://gyazo.com/d73b20a118db0088aab261c079613b00

我想将其显示为: https://gyazo.com/063990cd8741682f45b5a37ba594ff56

x轴数转移到右侧多一点。有可能做到这一点吗?

1 个答案:

答案 0 :(得分:3)

我认为您必须修改xticks位置,如:

import matplotlib.pyplot as plt
import numpy as np

# test data
eval = np.random.randint(0,3,10)
bin = np.arange(4) # len + 1 

figure=plt.figure(1)
plt.hist(eval,bins=bin, facecolor='blue',edgecolor='black')
# shift ticks by .5
plt.xticks(bin-.5, bin)
plt.xlabel('evaluation')
plt.ylabel('No of problems')
plt.title('Evaluations Distribution Histogram')
plt.show()

enter image description here