python - 缩放包含日期的 x 轴值 - 不显示日期(matplotlib)
问题描述
我正在尝试缩放包含日期的条形图的 x 轴。我也使用了set_major_locator()
matplotlib 的功能。这是我的代码:
def plot_var(country):
"""
Plots a bar chart of the given variable over the date range
"""
y = confirmed_data[confirmed_data['Country_Region']==country]["TargetValue"]
x = confirmed_data[confirmed_data['Country_Region']==country]['Date']
plt.figure(figsize=(12,4))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=30))
plt.title("Plot of \"ConfirmedCases\"",fontsize=18)
plt.bar(x=x,height=y,color='blue')
plt.grid(True)
plt.xticks(fontsize=14,rotation=45)
plt.yticks(fontsize=14)
plt.show()
但是,我的图表在 x 轴上没有显示任何值。我不明白为什么会这样。希望你能帮我解决这个问题!
PS 我已经包含了相同的必要库(matplotlib.pyplot 和 matplotlib.dates)。
解决方案
推荐阅读
- android-workmanager - 当 onStopped 是 final 时,如何在我的 CoroutineWorker 中执行清理代码?
- java - 使用 Hamcrest 检查某些元素是否在集合中
- python - 游戏;Surface 上的 Alpha gestion 问题
- php - 除日期外,将 PHP 生成的 DataPeriod 列表拆分为月和年
- ios - 从另一个 ViewController 中删除子视图
- arrays - Delphi 数组与对象
- javascript - 正确测试 Vue 组件中的事件处理程序
- javascript - Vue JS:从输入中重置/清除值
- java - 如何在多个微服务中管理安全上下文
- java - 如何使 IntellijIDEA 与 Java 12 一起工作