Difference in plotting with different matplotlib versions

前端 未结 1 623
夕颜
夕颜 2020-11-21 05:47

A colleague of mine handed me a script that is used to collect data from a database and plot it. When I used the script myself, the plots do not look the same, and it has to

相关标签:
1条回答
  • 2020-11-21 06:27

    The data is read in as strings. In matplotlib 2.0 those were automatically converted to floating point numbers such that they can be plotted.

    In matplotlib 2.1, categorical plots have been introduced. This now allows for something like

    plt.plot(["apple", "banana", "cherry"], [2,1,3])
    

    While this is of course great for certain applications, it breaks the previous option of plotting strings that are convertable to floats. I guess this if fine, it just gives the user the responsibility to do the conversion himself.

    In this case you would want to do this conversion like

    values = [None if v is '' else float(v) for v in values]
    

    In case you already have a numpy array: np.array(values).astype(float)

    In general, one can use numpy.loadtxt to read files into float arrays. If the file contains dates, usage of a converter as in reading a comma-delimited file with a date object and a float with Python would be possible.

    Another option to read in text files would be pandas.read_csv.

    0 讨论(0)
提交回复
热议问题