Keywordshow to plot data in pythonpython how to plot datahow to plot graph in python
Pandas is a very popular Python data manipulation library. Learn how to use its API to plot data.
In a series of articles about Python-based drawing libraries, we will conduct a conceptual study of drawing using Pandas, a very popular
Python data manipulation library. Pandas is a standard tool in Python for scalable data conversion. It has also become a popular method for importing and exporting data from CSV and Excel formats.
In addition, it also contains a very good drawing API. This is very convenient. You have
stored the data in a Pandas DataFrame, so why not use the same library for drawing?
In this series, we will make the same multiple bar histograms in each library so that we can compare how they work. The data we use is the result of the British election from 1966 to 2020:
Self-drawn data
Before continuing, please note that you may need to adjust the Python environment to run this code, including:
Running the latest version of Python (instructions for Linux, Mac and Windows)
Make sure you are running a Python version compatible with these libraries
The data is available online and can be imported using Pandas:
import pandas as pddf = pd.read_csv('https://anvil.works/blog/img/plotting-in-python/uk-election-results.csv')
Now we are ready. In this series of articles, we have seen some impressively simple APIs, but Pandas is sure to win.
To draw a histogram grouped by year and each party on the x-axis, I just need to do this:
import matplotlib.pyplot as pltax = df.plot.bar(x='year')plt.show()
With only four rows, this is definitely the best multi-bar histogram we created in this series.
I use the data in a wide format, which means that there is a column for each party:
This means that Pandas will automatically know how I want to group. If I want to group differently, Pandas can easily reorganize the DataFrame.
Like Seaborn, Pandas' drawing function is an abstraction on top of Matplotlib, which is why it is necessary to call Matplotlib's plt.show() function to actually generate the drawing.
It looks like this:
It looks great, especially if it is so simple! Let's style it to make it look like Matplotlib's example.
Adjust style
We can easily adjust the style by accessing the underlying Matplotlib method.
First, we can color the histogram by passing the Matplotlib color table to the drawing function:
This is almost the same as the Matplotlib version above, but only 8 lines of code are used instead of 16 lines! My inner code master is very happy.
Abstraction must be escapeable
Like Seaborn, the ability to access the Matplotlib API down to make detailed adjustments is really helpful. This is a good example of an abstract emergency exit that makes it both powerful and simple.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.