简体   繁体   中英

Python Matplotlib X-axis label dual axis with dataframe

I've got a dual axis bar and line plot using matplotlib. I read the data in as a dataframe,

[WEEK       SIGNUPS APPLICATIONS    PRECOURSE_WORK  QUALIFIED   ENROLLED    SPEND
 2019-10-07 5674    2938            2220            106         2           77581.67
 2019-10-14 4538    2225            2309            567         204         61258.08
 2019-10-21 3865    1997            1801            121         39          53700.58
 2019-10-28 3559    1886            1641            162         39          53543.28
 2019-11-04 3782    1946            1980            190         109         49495.64
 2019-11-11 4033    2035            1568            118         109         49952.17
 2019-11-18 3999    2009            1537            83          77          58545.72
 2019-11-25 6170    3322            1660            110         61          52332.4
 2019-12-02 5189    2658            7041            73          30          56727.55
 2019-12-09 4631    2497            7904            174         116         60977.49
 2019-12-16 4935    2501            3492            108         82          68179.54
 2019-12-23 5289    2603            1983            80          38          76956.81
 2019-12-30 5843    3037            2150            90          80          76246.14
 2020-01-06 4194    1930            1619            74          57          46114.68]

My code works and produces a graph (below) 双轴支出图

Here is my code

import matplotlib.pyplot as plt
from pylab import rcParams
from matplotlib import style
style.use('seaborn-paper')
#print(plt.style.available)
rcParams['figure.figsize'] = 20, 10
#plt.xticks(df[['WEEK']])
ax = df[['SPEND']].plot(kind='bar', color = 'lightblue')
ax.set_ylabel("Spend",color="blue",fontsize=20)
ax.set_xlabel('Weeks',color="blue",fontsize=20)
ax2 = ax.twinx()
ax2.plot(df[['SIGNUPS','APPLICATIONS','ENROLLED']].values, linestyle='-', marker='o', linewidth=4.0)
fmt = '${x:,.0f}'
tick = mtick.StrMethodFormatter(fmt)
ax.yaxis.set_major_formatter(tick)

When I uncomment the line plt.xticks(df[['WEEK']]) I get the following error

ConversionError Failed to convert value(s) to axis unit.

Can anyone help me out?

plt.xticks is expecting the tick locations to be specified and optionally the labels, from the docs the signature is

xticks(ticks, [labels], **kwargs)

So when you do

plt.xticks(df[['WEEK']])

It is trying to interpret the dates in the 'WEEK' column as the locations for the ticks. What you want to do instead is use plt.set_xticklabels which expects only the labels be specified, ie

plt.set_xticklabels(df[['WEEK']])
# or
plt.set_xticklabels(df[['WEEK']].values)

Although you may also need to manually covert the values to strings, depending on how they are defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM