[英]Set x axis labels for joyplot
I have written the code below to visualise a joyplot.我写了下面的代码来可视化一个joyplot。 When trying to change the x axis labels using
axes.set_xticks
, I get the error: AttributeError: 'list' object has no attribute 'set_xticks'
当尝试使用
axes.set_xticks
更改 x 轴标签时,出现错误: AttributeError: 'list' object has no attribute 'set_xticks'
import joypy
import pandas as pd
from matplotlib import pyplot as plt
data = pd.DataFrame.from_records([['twitter', 1],
['twitter', 6],
['wikipedia', 1],
['wikipedia', 3],
['indymedia', 1],
['indymedia', 9]], columns=['platform','day'])
# Get number of days in the dataset
numdays = max(set(data['day'].tolist()))
# Generate date strings from a manually set start date
start_date = "2010-01-01"
dates = pd.date_range(start_date, periods=numdays)
dates = [str(date)[:-9] for date in dates]
fig, axes = joypy.joyplot(data,by="platform")
axes.set_xticks(range(numdays)); axes.set_xticklabels(dates)
plt.show()
The expected output should look something like the following but with the dates from dates
as ticklabels.预期输出应类似于以下内容,但将日期中的
dates
作为刻度标签。
Since joypy.joyplot(..)
returns a tuple of figure
, axes
and axes
should be list of axes, you probably want to set the labels for the last axes,由于
joypy.joyplot(..)
返回一个figure
元组, axes
和axes
应该是轴列表,您可能希望为最后一个轴设置标签,
axes[-1].set_xticks(range(numdays))
axes[-1].set_xticklabels(dates)
To make date plots with python matplotlib do you should use plot_date function.要使用 python matplotlib 制作日期图,您应该使用 plot_date 函数。
fig, ax = plt.subplots()图, ax = plt.subplots()
ax.plot_date(dates, data1, '-') ax.plot_date(日期,数据1,'-')
I put the complete example in pastebin, follow the link: https://pastebin.com/sVPUZaeM我把完整的例子放在 pastebin 中,点击链接: https : //pastebin.com/sVPUZaeM
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter
from random import randrange, random
from datetime import datetime
#generate date list
start_date = np.datetime64('2010-01-01').astype(datetime)
numdays = 10
dates = pd.date_range(start_date, periods=numdays)
#Generate data example
data1 = [(random()+idx)**1.2 for idx in range(len(dates))]
data2 = [(random()+idx)**1.5 for idx in range(len(dates))]
#plot
fig, ax = plt.subplots()
ax.plot_date(dates, data1, '-')
ax.plot_date(dates, data2, '-')
#set the label for x and y and title
plt.title('Matplot lib dates wc example')
plt.xlabel('Dates')
plt.ylabel('Random values example')
#date format
ax.fmt_xdata = DateFormatter('%Y%m%d')
ax.grid(True)
fig.autofmt_xdate()
plt.show()
Python version tested successfully: 2.7.12 Python版本测试成功:2.7.12
This code generates: this follow plot此代码生成:此跟随图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.