简体   繁体   English

Python MatPlotLib刻度线

[英]Python MatPlotLib tick marks

Please help me figure out these tick marks. 请帮助我找出这些刻度线。 I have been working on this for hours and I can't find a solution. 我已经为此工作了几个小时,但找不到解决方案。 This data set is a collection of average house prices for various cities, from 1996-04 to late 2018. Here is my code: 该数据集收集了1996-04年至2018年末各个城市的平均房价。这是我的代码:

# Plotting Hot Springs housing value over time
fig=plt.figure(figsize=(20, 8), dpi= 80, facecolor='w', edgecolor='k')
ax=plt.axes
plt.plot(syrHST['71913'],color='blue')
plt.plot(syrHST['71901'],color='red')
plt.xticks(rotation='vertical', fontsize=20)
plt.ylabel('Average Housing Value', fontsize=20)
plt.gca().legend(('71913','71901'), fontsize=20, loc='upper left')
fig.suptitle('Hot Springs', fontsize=40)
plt.show()

Here's what it's showing me: 这是向我展示的内容:

Obviously those tick labels along the x-axis are no good. 显然,那些沿x轴的刻度标签是不好的。 I need to find a way to reduce the number of labels to about 22 so they can be read clearly. 我需要找到一种方法将标签的数量减少到22个左右,以便可以清晰地阅读它们。 I have tried rotating the labels, but that doesn't help. 我尝试旋转标签,但这无济于事。 Please help me. 请帮我。

You can set the ticks to some output of np.arange using something like this: 您可以使用以下方法将tick设置为np.arange某些输出:

def aranger(cols, n_ticks):
    return np.arange(cols.min(), cols.max(), (cols.max()-cols.min())/n_ticks)

# Plotting Hot Springs housing value over time
fig=plt.figure(figsize=(20, 8), dpi= 80, facecolor='w', edgecolor='k')
ax=plt.axes
index = aranger(syrHST[['71913','71901']], 10)
plt.plot(syrHST['71913'],color='blue')
plt.plot(syrHST['71901'],color='red')
plt.xticks(index, rotation='vertical', fontsize=20)
plt.ylabel('Average Housing Value', fontsize=20)
plt.gca().legend(('71913','71901'), fontsize=20, loc='upper left')
fig.suptitle('Hot Springs', fontsize=40)
plt.show()

Without dummy data I can't test that, but it's the general pattern I think you can use for this problem. 没有虚拟数据,我无法测试,但这是我认为可以用于此问题的一般模式。

Your dates are strings. 您的日期是字符串。 They are hence interpreted as categories and each category receives its own label. 因此,它们被解释为类别,并且每个类别都有其自己的标签。 What you want to do is to convert them to an actual date. 您要做的是将它们转换为实际日期。

df.index = pd.to_datetime(df.index)

If you then plot a column from the dataframe it would show you some automatic number of dates (not more than 9 along the axis in most cases). 如果再从数据框中绘制一列,则会显示一些自动日期(大多数情况下,沿轴的日期不超过9)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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