Pyhton plot

Some commonly used library and tips that draw graphs by python such as the bar graph, line graph, gant chart. etc.bar

The benifits of using the pyplot instead of the google chart is that you can edit the figure easily. Such as the size of the figure and the style of the figure. We may use the google sheet to get some general feelings about the orignal data and then use the python plot to draw the figure that will be used in the paper.

Style

This is the repo that shows some basic python based graph. The detailed explanation can be found as follows:

Default bar graph

This exmpale shows how to draw the bar graph and how to add specific error bar for each bar.

Splited bar graph

This example shows how to draw a bar graph with the cut off between the bar, if we draw the whole bar, the difference between different bar might be insignificant. If we reset the start point of the y axis, the error bar might become too obvious, this figure is a kind of trade off when your data is not good enough.

Accumulated bar graph

Gant chart

This is an example about the gant chart, it is an important type that shows the execution time for the each component of the MPI program. Be careful about the meaning of the x and y values in the broken-barh the xranges contains a list of tuple, each one defines the start point and the end point of the bar, then the y range defines the height of the bar.

Line graph

This is a simple graph about the line chart.

Line with the error region

This is used to show the case where there are errors for prediction results, we can show some uncertainty of the prediction, which can provides better prediction visualization results.

2d grid

This is an example for the grid graph. It basically visualize a 2d matrix, the color of each cell is the value in the matrix.

2d surface

subfigures

The subfigures use another different set of api to controrl things, if we do not set numbers in the subplots, that returned axs is one element, if we set numbers here, it returns a list.

Color

alpha parameter can adjust tranparency of the color, it can make the color more soft and looks comfortable, such as using 20% for the default red color can change it into the soft pink color.

Legend

In most cases, we can use the defualt one associated with the graph

We can also use the customized legend.

Such as this

https://stackoverflow.com/questions/39500265/how-to-manually-create-a-legend

Axis

The axis is different if using the subplot to draw multiple figures in one large figure.

Size and font

use the bbox_inches='tight' is one prefered option. The saving figure does not contains extra blank in this way.

Output

png and the pdf.

Save the figure as the pdf if it is possible.
If we can not render the pdf in the place that use this figure, set the png, but pay attention to the resolution of the png, we want to provide enough resolution here.

Scatter plot with density

One useful figure is to draw the scatter plot, if there are more dots at specific region, then there should be a higher density. The original way is just plot that figure using the scatter. But it could not show the density if all scattered points are same. A good way is to set the alpha value as 0.1, so when there are more points, the color at specific has a higher density value. Which can show the graph in a intuitive way. However, one drawback for this kind of figure is that, if there are large number of points, drawing figure like this is time consuming.

One way is to use the 2d histogram to draw this kind of figure. When there are large number of dots at a specific region, then highlight it and give it a higher density.

This is a good exmaple:

https://github.com/wangzhezhe/VisPerfExp/blob/main/logparservtkm2.0/parser_particles_draw_histogram.py

Small trick between plot and barchart

It seems that the plot chart can process the x axis automatically. Which means that we do not take care about how to map the value of x axis to the size of the figure, it will be adjusted automatically. For the bar chart, we can not do that. If we use the same x axis and y axis used in previous plot chanrt, we need to map that actual x axis into the position of the figure size. For example, if the data range of x axis is from 0 to 1000, than we need to map the x axis to figure size at horizental * x/1000 to get the correct position of the figure. Be careful about this trick. Otherwise, the x position of the figure is in correct.

EdgeColor

When drawing bar chart with small region, the edgecolor should be set as none. Otherwise, the edge might be overlapped together when two bars are close to each other, this might cause some visual artifacts and make data looks bad.

Log scale

Sometimes we might adopt log scale to make the graph more clear. Be careful about the axis tick of log scale, it should still be the original value before applying the log. For example, assuming the x axis is 2 4 8 … if we do not use log scale, the position of 2 is at place where there are 2 unit, 4 is labeled at place where there are 4 unit etc. After using log scale, such as base of 2, we are still using same tick, such as 2 ,4, 8 … The only difference is that we label them at different position. 2 is labeled at position where there is unit 1, 4 is labeled at position where there is unit 2…

This is similar for y scale, we still use the original value (the actual y value when we set the tick), the only difference is the tick position, the tick position after using log scale changes to the log(y), where y is the original tick position instead of the origianl tick value. Be careful about this !!!

推荐文章