Creating a Clustered Bar chart with Matplotlib - python

This is extremely trivial, so I apologize!
I'm just getting into matplotlib and pandas and I think I'm over complicating it...
I'm trying to create a clustered bar chart (like the one below).
The dataframe I a working with is structured like this:
I want to create a clustered bar chart where the x-axis is days of the week (df['Days of Week']), the y axis is count, and the categories of what is being counted are Type A and Type B (determined by df['Type']).
From the googling I am doing, my code is long and complicated... but I feel like this is easier than I'm making it...
Any help appreciated!

Try this:
new_df = (df.groupby('Days of Week')['Type']
.value_counts()
.unstack()
)
new_df.plot.bar()

Related

MACD plot in python similar to tradingview figure

Here is an example of MACD plot using python.
https://code.luasoftware.com/tutorials/algo-trading/python-mplfinance-plot-yfinance-candle-chart-moving-average-macd-and-volume/
I'd like the result (including colors and crossover points, etc.) to be the same as that of tradingview.com
https://www.tradingview.com/script/NDH8bJow-MACD-Colors-Signals/
Could anybody show the python so that it is the same as that of tradingview.com?
Could anybody show the python so that it is the same as that of tradingview.com?
I don't think anyone is going to want to write your code for you.
However what I would recommend is that you work through the mplfinance tutorials on Customizing the Appearance of Your Plots and especially teach yourself about mplfinance styles which shows how to modify the overall color scheme of your plot. See also mplfinance style sheets reference.
For example, to achieve a color scheme similar to trading view you could use the "market colors" from style 'yahoo' and the combine that with mplfinance style 'nightclouds'. Something like this:
mc = mpf.make_marketcolors(base_mpf_style='yahoo')
s = mpf.make_mpf_style(base_mpf_style='nightclouds',marketcolors=mc)
Then when you call mpf.plot() specify kwarg style=s
hth

Creating a scatter plot from a dataframe with dates on x-axis creates overlap

So I have a dataframe that consists of several variables, such as the amount of comments from an instagram page and the upload date of each post. For this, I used the package Instascrape
Now I want to make a scatter plot from these two variables, with the date on the x-axis and amount of comments on the y-axis using the following command:
plt.scatter(posts_df["upload_date"], posts_df["likes"])
plt.show()
Now I face the following problem, I get the scatter plot but the dates are overlapping. I believe that this is due to the fact that it shows every date on the x-axis, if they are close to each other the dates are overlapping.
I tried to convert it using:
plt.scatter(posts_df["upload_date"], posts_df["comments"])
plt.show()
But that did not change anything. The dates look like this: upload_date
0 2020-12-26 01:23:57
how can I solve this?
I solved it by using simply the command:
plt.gcf().autofmt_xdate()

Using sns to plot a JIRA like created v/s resolved

I'm struggling with a basic qs.
My dataframe looks like this:
q = pd.DataFrame(data=[[28,50,30],[29,40,40],[30,30,30]],columns=['sprint','created','resolved'])
I want to plot a barplot with sprint & for each sprint I want to plot created & resolved.
Can someone help out how this can be done?
Pandas can use matplotlib in order to display information of pandas objects. In your case it seems for me the easiest way to go with pandas plots.
The following gives you a plot with only the information about the sprints as a bar plot
df.sprint.plot(kind='bar')
This gives you a plot for each sprint showing the created and resolved tasks
df.groupby('sprint').plot(y=['created','resolved'], kind='bar')

marker style by third variable

Might seem like a repeat question, but the solution in this post doesn't seem to work for me.
I have a bunch of data I want to plot as lines/curves, and another dataset linked to the curves consisting of XYZ data, where Z represents a labeling variable for the curves.
I've got some example code here with some XY data, and labels for anyone wanting to replicate what I'm doing:
plt.plot(xdata, ydata)
plt.scatter(xlab, ylab, c=lab) # needs a marker function adding
plt.show()
Ideally I want to add some kind of unique marker based on the label values; 0.1,0.5,1,2,3,4,6,8,10,20. The labels are the same for each curve.
I have over 100 curves to plot, so something quick and effective is needed. Any help would be great!
My current solution would be to just split the data by labelling values, and then plot separately for each one (long and messy in my opinion). Figured someone might have a more elegant solution here.
I'm guessing you could do this with a dictionary... but I might need some help doing that!
Cheers, KB
Matplotlib does not accepts different markers per plot.
However, a less verbose and more robust solution for large dataset is using the pandas and seaborn library:
Additionally you can use the pandas.cut function to plot bins (Its something I regularly need to produce graphs where I can use a third continuous value as a parameter). The way to use it is :
import pandas as pd
import seaborn as sns
url = 'https://pastebin.com/raw/dwGBLqSb' # url of paste
df = pd.read_csv(url)
sns.scatterplot(data = df, x='labx', y='laby', style='lab')
and it produces the following example:
If you have something more advanced labelling you could also look at LabelEncoder of Sklearn.
Hopefully, I've edited enough this answer not to offend don't post identical answers to multiple questions. For what is worth, I am not affiliated with seaborn library in any way nor am I trying to promote anything. The only thing I am trying to do is help someone with a similar problem that I've come across and I couldn't find easily a clear answer in SE.

How to create a Bar Chart with Python Pandas with Greek Labels

i am trying to make a bar chart using pandas and matplotlib. The data is depicted in the dataframe (champs_by_number_of_matches) bellow and it is Greek.
by typing: champs_by_number_of_matches.plot(kind = 'bar')
i get the graph bellow. How can i get the x-axis labels to show Greek?
This is a frequent problem i have encountered and i dont want to convert the labels to "Greeklish".
Thank you.

Categories

Resources