Data visualization explained (Part 4): Essential Python review

In my data visualization thread. See the following:
Up to this point in my data visualization series, I've covered the basics of optical design. These principles are important to understand before designing and building a visualization, as they ensure that the underlying data is done justice. If you haven't already, I highly encourage you to read my previous articles (linked above).
At this time, you are ready to start building our vision. I'll cover various ways to do that in future articles – and in the spirit of data science, many of these methods will require programming. To make sure you're ready for this next step, this article will have a brief review of the essentials of Python, followed by a discussion of their relevance to Coding Data Vialivezis.
Basics-expressions, variables, functions
Expressions, variables, and functions are the building blocks of all Python code – and really, code in any language. Let's see how they work.
To show
A to encourage a statement that evaluates to a specific value. The simplest expression is a constant value of any type. For example, below are three simple expressions: the first is a number, the second is a string, and the third is a float.
7
'7'
7.0
Many complex expressions often arise from mathematical functions. We can add, subtract, multiply, or divide different numbers:
3 + 7
820 - 300
7 * 53
121 / 11
6 + 13 - 3 * 4
By definition, these expressions are analyzed with a single value by Python, after the Mathematical of Operations defined by Pemdas of Acronany (Parentheses, Expendent, Multiplication, division, subtraction) [1]. For example, the last expression above evaluates a number 7.0. (See why?)
Variable
Speeches are good, but they are not very useful in themselves. When the program, you usually need to save a number of certain expressions to use in the later parts of our program. A – don't match it is a container that holds the value of the expression and allows you to retrieve it later. Here are the same expressions as in the first example above, but this time with their value stored in various variables:
int_seven = 7
text_seven = '7'
float_seven = 7.0
Variables in Python have several important properties:
- Dynamic a noun (The word to the left of the equal sign) must be a single word, and cannot start with a number. If you need to include multiple words in your variable names, the convention separates them from the bottom (as in the examples above).
- You don't need to specify a data type when working with variables in Python, as you may be used to doing when you encounter programs in a different language. This is because Python a typing with power language.
- Some programming languages distinguish between announcement once share of difference. In Python, we simply assign variables on the same line that we declare them, so there is no need to separate them.
When declared, Python will always evaluate the expression on the right side of the equals sign for a single value before representing it. (This links back to how Python evaluates complex expressions). Here is an example:
yet_another_seven = (2 * 2) + (9 / 3)
The variable above is assigned a value 7.0not a compound speech (2 * 2) + (9 / 3).
Activities
A work can be thought of as a type of machine. It takes something (or many things) in, runs some code that modifies the things passed, and returns exactly some value. In Python, functions are used for two main reasons:
- Manipulate interest input variables and come up with the outputs we need (much like math functions).
- To avoid duplication of code. By packaging code inside a function, we can simply call the function whenever we need to run that code (as opposed to writing the same code over and over again).
The easiest way to understand how to define functions in Python is to look at an example. Below, we have written a simple function that doubles the value of a number:
def double(num):
doubled_value = num * 2
return doubled_value
print(double(2)) # outputs 4
print(double(4)) # outputs 8
There are some important points about the example above that you should make sure you understand:
- This page
defThe keyword tells Python that you want to define a function. Name directly behinddeffunction name, so the function above is calleddouble. - After the name, there is a parent array, inside which you put the parameters of the function (a nice name that just means the input of the function). IMPORTANT: If your function doesn't need any parameters, you still need to include parentheses—just don't put anything inside them.
- At the end of the
defIn the statement, a colon must be used, otherwise Python will not be happy (ie, it will throw an error). Together, every line withdefThe statement is called Work signature. - All lines after
defThe statement contains the code that performs the function, given a single section inside. Together, these lines form Job Duty. - The last line of the function above is return statementwhich specifies the output of the function using
returnkeyword. The return statement does not have to be the last line of the function, but after it is always met, Python will exit the function, and no lines of code will be complicated. More complex functions can have multiple return statements. - You guys shout Work by writing their name, and put the desired input on the parents. If you call the function without input, you still need to input the learners.
Python and data visualization
So, let me address a question you may be asking yourself: Why all this Python review to begin with? After all, there are many ways you can visualize data, and they are actually not limited by knowledge of Python, or even programming in general.
This is true, but as a data scientist, you will probably need to program at another point – and within programming, the language you use will most likely be python. When you've already assigned a pipeline of data cleaning and analysis to data engineers on your team, it pays to know that they will quickly and effectively transform it into a set of tangible and visible insights.
Python is important to know for data visualization in general speaking, for several reasons:
- It is an accessible language. If you are just transitioning to Data Science and Visualization work, it will be much easier to program Python visualizations than it will be working with low-level tools like D3 in JavaScript.
- There are many different and popular Python libraries, all of which provide the ability to visualize data with code that builds directly on the Python Basics we learned above. Examples include MatPlotlib, ocean, coming, and vega-altair (formerly known as altair). I will explore some of these, especially the altair, in future articles.
- In addition, the above libraries all integrate seamlessly into pandas, the base science library in Python. Data in pandas can be directly loaded into logic from these libraries to build visualizations; You usually won't even need to export or convert before you start visualizing.
- The basic principles discussed in this article may seem backward, but they go a long way in the wake of data:
- Computing expressions correctly and understanding those written by others is essential to ensure that you present accurate data representations.
- You often need to store specific values or sets of values for recent visualization entries – you'll need flexibility for this.
- Sometimes, you can save just visual perception in conversion for later use or display.
- More advanced libraries, such as votly and altair, allow you to call built-in (and sometimes even user-defined) functions to customize the visualization.
- A basic knowledge of Python will allow you to integrate your visualizations into simple applications that can be shared with others, using tools like dash for building and editing. These tools aim to simplify the process of building systems for new scientists new to systems, and the basic concepts covered in this article will be enough to get you started.
If that's not enough to convince you, I would urge you to click on one of the links above and start checking out some of these visuals for yourself. Once you start seeing what you can do with them, you won't go back.
For my part, I will return in the next article to present my tutorial on visual design. (One or more of these tools may make an appearance.) Until then!



