用matplotlib绘制python日期:x标签向右移动

时间:2017-01-05 18:48:45

标签: python datetime matplotlib

当我使用Matplotlib X标签绘制日期时,向右移动。只有超过10-15个条目才会发生。我认为问题在于我如何处理python日期,但无法弄清楚我的代码到底出了什么问题。我将非常感谢你的帮助。

我得到这种格式的日期列表:

mydates = [datetime.datetime(2016, 12, 17, 8, 0), datetime.datetime(2016, 12, 4, 8, 0), etc.]

我的代码:

x = mydates
x = date2num(x)
y = female_values
z = male_values

plt.figure(figsize=(10, 4.5))
matplotlib.rcParams.update({'font.size': 10})
ax = plt.subplot(111)
width = 0.2
opacity = 0.5
ax.bar(x, y, width=width, color='orange', align='center', alpha=opacity)
ax.bar(x+width, z, width=width, color='green', align='center', alpha=opacity)
ax.xaxis_date()
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m'))
plt.xticks(x+(width/2), rotation=60)
plt.legend(['Female', 'Male'], loc='upper left')
timestr = time.strftime("%Y%m%d-%H%M%S")
plt.savefig('myapp/static/charts/'+timestr+'.png')

情节:

X labels shift to the right

更多例子(14天):

enter image description here

15天:

enter image description here

1 个答案:

答案 0 :(得分:0)

问题只发生在2.0之前的matplotlib版本中。

来自更新matplotlib的公寓,解决方案是手动设置边距:

struct qnode
{
struct node *node;
struct qnode *next;
};

struct frontandrear
{
struct qnode *front;
struct qnode *rear;
};


struct node* newNode( int data )
{
struct node *new_node = (struct node*)malloc( sizeof( struct node ) );
new_node->data = data;
new_node->left = new_node->right = NULL;
return new_node;
}



void enQueue( struct frontandrear *fr, struct node* node )
{
struct qnode* new_node = (struct qnode*)malloc( sizeof( struct qnode ) );
new_node->node = node;

if (fr->rear == NULL)
    {
    new_node->next = NULL;
    fr->rear = new_node;
    fr->front = new_node;

    }
else
    {
    new_node->next = fr->rear;
    fr->rear = new_node;
    }
}

struct node* deQueue( struct frontandrear *fr )
{
if (fr->front == NULL)
    {
    return NULL;
    }
struct node* num = fr->front->node;
fr->front = fr->front->next;

if (fr->front == NULL)
    {
    fr->front = fr->rear = NULL;
    }
return num;
}

void printLevelOrder( struct node* root )
{
struct frontandrear *frontandrear = (struct frontandrear*)malloc( sizeof(     struct frontandrear ) );
frontandrear->front = frontandrear->rear = NULL;

struct node *temp = root;

while (temp != NULL)
    {
    printf( "%d\t", root->data );
    if (temp->left)
        enQueue( frontandrear, root->left );
    if (temp->right)
        enQueue( frontandrear, root->right );
    temp = deQueue( frontandrear );
    }
}


int main( )
{

struct node *root = newNode( 1 );
root->left = newNode( 2 );
root->right = newNode( 3 );
root->left->left = newNode( 4 );
root->left->right = newNode( 5 );

printf( "Level Order traversal of binary tree is \n" );
printLevelOrder( root );
}

这意味着应该有一个4%的轴大小的空间左右数据。你当然可以尝试其他价值观。