对我来说,强迫左y轴开始0%
而不是从一个容易48%
吗?
这是我的代码:
fig, ax1 = plt.subplots(figsize=(18,12))
fig.autofmt_xdate()
#update ticks here when graph gets too large
tick_spacing = 5
#y_tick_spacing = 20
ax1.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
color = 'tab:blue'
ax1.set_xlabel('Date',fontsize=18)
ax1.plot(events_df['dt'], events_df['match_rate'], color=color)
ax1.tick_params(axis='y', labelcolor=color)
#ax1.set_ylim([0,100])
ax1.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
#ax1.yaxis.set_major_locator(ticker.MultipleLocator(y_tick_spacing))
ax2 = ax1.twinx()
color = 'tab:red'
ax2.plot(events_df['dt'], events_df['score'], color=color)
ax2.tick_params(axis='y', labelcolor=color)
ax2.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
fig.tight_layout()
plt.show()
我尝试添加,ax1.set_ylim([0,100])
但似乎没有效果,而是挤压了一些东西:
我对从开始的y轴感到满意35%
。
这是我希望从相同数据的Google表格中获取的内容:
检查此代码:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.ticker as mtick
import numpy as np
events_df = pd.DataFrame({'dt': pd.date_range(start = '2020-02-01', end = '2020-05-16')})
events_df['match_rate'] = 100*(0.48 + (0.9 - 0.48)*np.random.rand(len(events_df.index)))
events_df['score'] = 100*(0.35 + (1 - 0.35)*np.random.rand(len(events_df.index)))
fig, ax1 = plt.subplots(figsize=(18,12))
fig.autofmt_xdate()
tick_spacing = 5
ax1.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
color = 'tab:blue'
ax1.set_xlabel('Date',fontsize=18)
ax1.plot(events_df['dt'], events_df['match_rate'], color=color)
ax1.tick_params(axis='y', labelcolor=color)
ax1.yaxis.set_major_formatter(mtick.PercentFormatter())
ax1.set_ylim([0, 100])
ax2 = ax1.twinx()
color = 'tab:red'
ax2.plot(events_df['dt'], events_df['score'], color=color)
ax2.tick_params(axis='y', labelcolor=color)
ax2.yaxis.set_major_formatter(mtick.PercentFormatter())
fig.tight_layout()
plt.show()
这给了我这个情节:
由于我没有您的数据,因此我会随机生成一个数据以进行绘图;用您的替换它们。
在我的情况下,'match_rate'
和'score'
列分别float
在范围(48-90)
和中(35-100)
。所以我设定
ax1.yaxis.set_major_formatter(mtick.PercentFormatter())
告诉matplotlib y轴具有百分比格式,然后用
ax1.set_ylim([0, 100])
这取决于'match_rate'
and'score'
列中的数据类型:了解它们将很有用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句