【问题标题】:matplotlib histogram axis formattingmatplotlib 直方图轴格式
【发布时间】:2017-04-27 09:56:16
【问题描述】:

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

def histogram(self):

    conn = sqlite3.connect('tooldatabase.db')
    c = conn.cursor()
    c.execute('PRAGMA foreign_keys = ON')
    c.execute("SELECT evaluation from evaluations")
    evaluations=c.fetchall()
    print(evaluations)
    minimum=min(evaluations, key = lambda t: t[0])
    maximum=max(evaluations, key = lambda t: t[0])
    print(minimum,maximum)
    eval=[]
    for (y,) in evaluations:
        eval.append(y)
    bin=[]
    for x in range(1,maximum[0]+2):
        bin.append(x)

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

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

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

x 轴数字向右移动了一点。有没有办法做到这一点?

【问题讨论】:

标签: python matplotlib histogram


【解决方案1】:

我认为你必须修改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()

【讨论】:

  • plt.xticks(bin-.5, bin) TypeError: unsupported operand type(s) for -: 'list' and 'float'
  • 试试plt.xticks(np.array(bin)-.5, bin)
猜你喜欢
  • 2019-02-20
  • 2018-12-03
  • 2021-10-07
  • 1970-01-01
  • 2014-03-17
  • 1970-01-01
  • 1970-01-01
  • 2015-06-22
  • 2020-05-20
相关资源
最近更新 更多