简体   繁体   English

如何更正matplotlib图中重叠的刻度标签?

[英]How to correct overlapping tick labels in matplotlib plot?

I have the dataframes ready to plot, but when I use matplotlib to plot these data the lines are not correct and do not show the trend.我已经准备好绘制数据框,但是当我使用 matplotlib 绘制这些数据时,线条不正确并且没有显示趋势。

for example, the first graph should be a curly line, however, I got a straight line plotted in the graph.例如,第一个图形应该是一条卷线,但是,我在图形中绘制了一条直线。

I wonder how to plot these lines correctly?我想知道如何正确绘制这些线? and fix both axis?并修复两个轴?

import pandas as pd
import datetime as dt
import pandas_datareader as web
import matplotlib.pyplot as plt
from matplotlib import style
import matplotlib.ticker as ticker
from bs4 import BeautifulSoup
import requests
import matplotlib.dates as mdates


url = 'https://www.federalreserve.gov/data.xml'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')


for chart in soup.select('chart'):
    
    series = {}
    index = []
    for s in chart.select('series'):
        series[s['description']] = []
        temp_index = []
        for o in s.select('observation'):
            temp_index.append(o['index'])
            series[s['description']].append(o['value'])
        
        if len(temp_index) > len(index):
            index = temp_index

    series['index'] = index
    max_len = len(max(series.values(), key=len))
    for k in series:
        series[k] = series[k] + ['No Data'] * (max_len - len(series[k]))
    df = pd.DataFrame(series).set_index('index')
    print(df)
    print('-' * 80)
    plt.figure()
    for i in df:
        
        plt.plot(df.index,df[i],label=chart['title'])
        plt.show()

在此处输入图片说明

  1. The dates are not a datetime format, so they are interpreted as strings, and they're all unique, which makes a mess of the axis.日期不是日期时间格式,因此它们被解释为字符串,并且它们都是唯一的,这使轴混乱。
    • df.index = pd.to_datetime(df.index) has been added df.index = pd.to_datetime(df.index)已添加
  2. The values in the columns are also strings, not numbers列中的值也是字符串,而不是数字
    • df = pd.DataFrame(series, dtype=float).set_index('index') will catch most of the columns, but there are some columns that still have stings, so can't be converted df = pd.DataFrame(series, dtype=float).set_index('index')会捕获大部分的列,但是有一些列仍然有刺,所以无法转换
    • print(df.info()) has been added. print(df.info())已添加。 Review and fix any column that is an object .查看并修复作为object任何列。 That means the column contains some strings and can't be converted to a float.这意味着该列包含一些字符串并且无法转换为浮点数。
      • Use [np.nan] instead of ['No Data'] , so the column can be set as a float, which will allow it to plot correctly.使用[np.nan]而不是['No Data'] ,因此可以将列设置为浮点数,这将使其能够正确绘图。
import numpy as np

url = 'https://www.federalreserve.gov/data.xml'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')


for chart in soup.select('chart'):
    
    series = {}
    index = []
    for s in chart.select('series'):
        series[s['description']] = []
        temp_index = []
        for o in s.select('observation'):
            temp_index.append(o['index'])
            series[s['description']].append(o['value'])
        
        if len(temp_index) > len(index):
            index = temp_index

    series['index'] = index
    max_len = len(max(series.values(), key=len))
    for k in series:
        # adding No Data is preventing the the column from being interpreted as a float
        series[k] = series[k] + [np.nan] * (max_len - len(series[k]))

    df = pd.DataFrame(series, dtype=float).set_index('index')  # added dtype=float
    df.index = pd.to_datetime(df.index)  # convert the index to a datetime format
    print(df)
    print(df.info())  # review the printed info, any column that isn't a float has strings in it the must be fixed

    print('-' * 80)
    plt.figure()
    for i in df:
        plt.figure(figsize=(9, 5))
        plt.plot(df.index, df[i])
        plt.title(f'{chart["title"]}\n{i}')
        plt.show()

在此处输入图片说明

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

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